我正在尝试编写一个简单的数字猜测程序。在这个特定的程序中,python 是在猜测用户正在考虑的数字,而不是相反。
我的代码返回 UnboundLoalError。
而且,这一切似乎都太长太麻烦了。我怎样才能缩短它?
谢谢你。
# Guesses a number
from random import randint
number = ""
def numberGuesser():
while True:
print "Think of a number between 1 and 50"
raw_input("Hit a key when ready")
number = randint(1,50)
answer = raw_input("Is your number: %d?. y/n> " % number)
if answer == 'y':
print "Great!"
raw_input("Hit a key to exit.")
elif answer == 'n':
greater_or_smaller()
else:
print "Please type y or n"
def greater_or_smaller():
while True:
greater_or_smaller = raw_input("Is\
your number greater or smaller than %d? g/s> " % number)
if greater_or_smaller == 'g':
number = randint(number, 50)
answer = raw_input("Is your number %d? y/n> " % number)
if answer == 'y':
print "Hooray!"
elif answer == 'n':
greater_or_smaller()
elif greater_or_smaller == 's':
number = randint(1, number)
answer = raw_input("Is your number %d? y/n> " % number)
if answer == 'y':
print "Hooray!"
elif answer == 'n':
greater_or_smaller()
numberGuesser()