0
Why my code doesn't work?
When my number does not match it say "You loose!",when match it say "You loose!" too.What is the problem? https://code.sololearn.com/cQv47TC0nfdD/?ref=app
6 ответов
+ 13
1. The 'for' loop serves no purpose. You don't need it.
2. You have to convert the user input to an integer to compare it with the random integer.
3. Don't use input(""). Just use input().
4. Try to make your code DRY (Don't Repeat Yourself). Avoid unnecessary repetition.
5. Watch your spelling 😁.
Here is my suggestion:
import random
#Choose a number from 1 to 10
value = random.randint(1,10)
number = int(input())
print("You chose", number)
print("Machine chose", value)
if number == value:
print("You win!")
else:
print("You lose")
Here is a shorter version:
#Choose a number from 1 to 10
from random import randint as ri
print("You chose", number := int(input()))
print("Machine chose", value := ri(1, 10))
print(f"You {'win!' if number == value else 'lose'}")
https://code.sololearn.com/c62aS2XEXkb4
+ 4
You have to change the input to int because you are comparing an integer value to a string value.
So your code should be:
import random
#Choose the number from 1 to 10
for x in range(1):
value = random.randint(1,10)
number = int(input(""))
if number == value:
print("You choosed", number )
print("Machine choosed", value )
print("You win!")
else:
print("You choosed", number )
print("Machine choosed", value )
print("You loose")
+ 1
You are trying to compare a string variable with a numeric variable ☺️
0
APIs must validate and sanitize user inputs to prevent common security vulnerabilities like SQL injection, cross-site scripting (XSS), or command injection attacks. Failure to properly validate inputs can lead to unauthorized access, data corruption, or exposure of sensitive information. See more detail here https://codesealer.com/risks-we-mitigate/
- 1
hey