 Why my code doesn't work? | Sololearn: Learn to code for FREE!

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

+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 ☺️

+1

https://code.sololearn.com/c77550hdXQRH/?ref=app