python猜数字的错误
importrandomsecret=random.randint(1,100)guess=0tries=0print"我有一个秘密"print"它在1到99之间,我给你...
import random
secret=random.randint(1,100)
guess=0
tries=0
print"我有一个秘密"
print"它在1到99之间,我给你6次机会"
while guess !=secret and tries < 6:
guess=input("你猜什么")
if guess < secret:
print"太低了,再猜"
elif guess > secret:
print"太高了,再来"
tries=tries+1
if guess == secret:
break
if guess == secret:
print "你猜对了,做得好"
else:
print"没机会了,下次努力"
print"这秘密是",secret
请详细的说出我的错误,我是初学者,这个是根据书本与孩子一起学编程里的,我一输入就直接告诉我答案了 展开
secret=random.randint(1,100)
guess=0
tries=0
print"我有一个秘密"
print"它在1到99之间,我给你6次机会"
while guess !=secret and tries < 6:
guess=input("你猜什么")
if guess < secret:
print"太低了,再猜"
elif guess > secret:
print"太高了,再来"
tries=tries+1
if guess == secret:
break
if guess == secret:
print "你猜对了,做得好"
else:
print"没机会了,下次努力"
print"这秘密是",secret
请详细的说出我的错误,我是初学者,这个是根据书本与孩子一起学编程里的,我一输入就直接告诉我答案了 展开
1个回答
展开全部
看你的代码,至少三处错误:
if guess < secret:
print"太低了,再猜"
tries=tries+1 #这里也应该把tries+1
elif guess > secret:
print"太高了,再来"
tries=tries+1 #这里应该缩进
elif guess == secret:
'''两个if合并成一行,不然执行不到第二个print "你猜对了,做得好",并改成elif,不然只要没猜 对,else就会执行'''
print "你猜对了,做得好"
break
else: #这里改成 if tries ==6:
print"没机会了,下次努力"
print"这秘密是",secret
if guess < secret:
print"太低了,再猜"
tries=tries+1 #这里也应该把tries+1
elif guess > secret:
print"太高了,再来"
tries=tries+1 #这里应该缩进
elif guess == secret:
'''两个if合并成一行,不然执行不到第二个print "你猜对了,做得好",并改成elif,不然只要没猜 对,else就会执行'''
print "你猜对了,做得好"
break
else: #这里改成 if tries ==6:
print"没机会了,下次努力"
print"这秘密是",secret
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询