I've made a python program that chooses between a random number or letter, the user then tries to guess it, but when the program chooses a number, and the correctly guesses the exact number, it returns with that the number the user inputted is incorrect.
This is my code:
import math
import random
import string
count = 0
numberroll = random.randint(1,10)
stringroll = random.choice(string.ascii_letters)
chooseroll = random.choice([numberroll, stringroll.lower()])
# print(chooseroll)
count = 0
if chooseroll == numberroll:
usernumberguess = input("Your guess: ")
if usernumberguess.isdigit():
print("It is a number!")
count = 1
else:
print("That is not a number...")
if usernumberguess.isdigit() == numberroll:
print("Correct number!")
count = 2
else:
print("Better luck next time!")
if chooseroll == stringroll.lower():
userstringguess = input("Your guess: ")
if userstringguess.isalpha():
print("It is a letter!")
count = 1
else:
print("That is not a letter...")
if userstringguess.lower() == stringroll.lower():
print("Correct letter!")
count = 2
else:
print("Incorrect! Better luck next time!")
# print(count)
Is there any way to fix this? Thanks in advance!
CodePudding user response:
This part if usernumberguess.isdigit() == numberroll:
may be faulty: isdigit()
returns True or False, and you check if it is the numberroll
. You can put that check whether the guess is correct within the case when the gues is a digit. You also need to turn your input, which is a string, into an int
.
if chooseroll == numberroll:
usernumberguess = input("Your guess: ")
if usernumberguess.isdigit():
print("It is a number!")
usernumberguess = int(usernumberguess)
count = 1
if usernumberguess == numberroll:
print("Correct number!")
count = 2
else:
print("Better luck next time!")
else:
print("That is not a number...")
I'd add some more code for debugging purposes so you can see what goes wrong:
import random
random.seed(0)
count = 0
numberroll = random.randint(0, 9)
print(f"the user has to guess: {numberroll}")
usernumberguess = input("your guess: ")
print(f"your guess is {usernumberguess}")
if usernumberguess.isdigit():
print("your guess is a number")
usernumberguess = int(usernumberguess)
count = 1
if usernumberguess == numberroll:
print("your guess is correct")
count = 2
else:
print("your guess is not correct")
else:
print("your guess is not a number")