Why my code doesn't work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

13th Apr 2021, 6:37 PM
Benwep
Benwep - avatar
6 Answers
+ 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
14th Apr 2021, 3:32 AM
David Ashton
David Ashton - avatar
+ 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")
13th Apr 2021, 6:48 PM
Haidar Mazen Hasan
Haidar Mazen Hasan - avatar
+ 1
You are trying to compare a string variable with a numeric variable ☺️
13th Apr 2021, 6:54 PM
Solo
Solo - avatar
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/
8th Nov 2023, 6:15 AM
Rtei Euwj
Rtei Euwj - avatar
- 1
hey
28th Jun 2022, 8:13 AM
the we are
the we are - avatar