Bra*_*eod -6 python python-2.7
我想在python中制作一个猜谜游戏.
from random import randint
print "\nI'm thinking of a number, you have to guess what it is.\n"
num = randint(1,100)
guess = 0
while guess != num:
guess = raw_input("Guess the number \n")
if guess < num:
print "Guess higher next time \n"
elif guess > num:
print "Guess lower next time \n"
elif guess == num:
print "That's correct \n"
Run Code Online (Sandbox Code Playgroud)
问题是无论我输入什么号码,我每次都会得到"猜数较低的数字".那有什么不对?
有了这个:
guess = raw_input("Guess the number \n")
Run Code Online (Sandbox Code Playgroud)
guess 是一个字符串,而不是一个数字.
你可以这样做:
guess = int(raw_input("Guess the number \n"))
Run Code Online (Sandbox Code Playgroud)
得到一个int.