Number Guesser Program Malfunction [duplicate]
Possible Duplicate:
Learning Python: If condition executing all the time
This Python code is for guessing a number from 1 to 100. There is nothing wrong with the 'guesser()' function, which I know because my program also has a "manual mode" that works perfectly. For some reason, it always assigns the 'guesss' value to 'low' and keeps doing so until it equals 100 and the program "Ragequits". It never does anything to 'high', which stays at its default throughout the execution. No matter what, it never executes the "winning" block. NOTE: The variables 'high' and 'low' start as 101 and 0 respectively, and 'guesser()' chooses a number between them. 'goOn' is used earlier in the code to determine if the person wants to play again.
num = raw_input('Enter the number which you want the computer to guess. ')
unguessed = True
while unguessed:
if high == low + 1 or high <= low:
print 'Waitafligginflagginminnit! You CHEATED! *Ragequits*'
goOn = False
unguessed = False
print ''
raw_input('Press Enter to continue.')
guesses = guesses + 1
guesss = guesser()
print 'I guessed', guesss
if guesss == num:
print 'Yay! I won in', guesses, 'guesses!'
开发者_如何学运维 again = raw_input('Just press enter if you want to play again. Otherwise...you know the drill. ')
unguessed = False
print '\n\n'
if again:
goOn = False
else:
print 'Awww...'
if guesss > num:
high = guesss
elif guesss < num:
low = guesss
You should use int(raw_input())
instead of raw_input
in order to retain the guess as an integer. raw_input()
alone returns a string, and so comparisons will not hold.
See these previously answered question, and this exact question as yours.
精彩评论