Something Something - 4 months ago 6
Python Question

From a guessing game in python, I can't get myself to get the correct one

This is my code:



from sys import exit
import random
number = random.randint(1, 10)
count = 0

def guess():

print ("Input a number 1 - 10")
guess = input()

if guess == "I give up":
print ("The correct number was", number,"!")
print ("You tried", count, "times before giving up!")
exit(0)
else:
if guess == (number):
print ("CORRECT!")
print (count, "failed attempts.")
exit(0)
else:
print ("WRONG!")
print ("Try again!")
global count
count += 1

while True:
guess()





If I run this, I can keep guessing but never get the correct number. I said I gave up, and it gave me the correct number. But I already guessed that number, so I don't know what's the problem.

Answer

You are taking the input as a string rather than an integer. Thus, you may be comparing "5" to 5, which is false. Instead, call if int(guess) == (number)