代码有点小bug需要指点下
import randomanswer = random.randint(1,100)
temp = input('请输出我让你猜的数字:')
guess = int(temp)
while guess !=answer:
temp = input('请重新输入:')
guess = int(temp)
if guess == answer:
print('恭喜你猜对了!')
else:
if guess > answer:
print('大了大了')
else:
print('小了小了')
print("不玩了游戏结束")
嗯,如果我想加一个当用户猜对的时候,输出用户猜测的数字应该怎么该? import random
answer = random.randint(1,100)
temp = input('请输出我让你猜的数字:')
guess = int(temp)
while guess !=answer:
temp = input('请重新输入:')
guess = int(temp)
if guess == answer:
print('恭喜你猜对了!')
else:
if guess > answer:
print('大了大了')
else:
print('小了小了')
print('正确答案:' + int(temp))
print("不玩了游戏结束")
叼辣条闯世界 发表于 2020-10-30 20:18
猜对的时候报错了 本帖最后由 hrp 于 2020-10-30 20:39 编辑
你while的循环条件是guess !=answer,你在循环里判断guess == answer是没意义的,因为能进入循环说明他们肯定不相等,所以这里用while...else...语句来写
import random
answer = random.randint(1,100)
temp = input('请输出我让你猜的数字:')
guess = int(temp)
while guess !=answer:
if guess > answer:
print('大了大了')
else:
print('小了小了')
temp = input('请重新输入:')
guess = int(temp)
else:
print('恭喜你猜对了!')
print("不玩了游戏结束") hrp 发表于 2020-10-30 20:36
你while的循环条件是guess !=answer,你在循环里判断guess == answer是没意义的,因为能进入循环说明他们肯 ...
可以了 感谢!
页:
[1]