from random import randint
x=(randint(0,9))
print "I'm thinking of a number between 1 and 10."
y = raw_input("What is your number? (Integer from 1 to 10)")
if y<x:
print "Too low!"
elif y>x:
print "Too high!"
elif y==x:
print "Spot On!"
sys.exit()
我如何循环它,让你一直猜直到你得到数字?
您可能需要为此目的研究while
循环。请查看文档了解详细信息,并检查已经提供的有用代码片段的答案。
当你得到正确的数字时就中断
from random import randint
x=(randint(0,10))
print "I'm thinking of a number between 1 and 10."
while True:
y = int(raw_input("What is your number? (Integer from 1 to 10)"))
if y<x:
print "Too low!"
print "Let's try again"
elif y>x:
print "Too high!"
print "Let's try again"
elif y==x:
print "Spot On!"
break
将y转换为int
,如果您想包含10,则必须在randint
函数中迭代到10而不是9