求助大神 这个程序又进行不下去了
import randomanswer=random.randint(1,10)
print('hello hello')
temp=input('不妨说说你心里想的数字')
times=1
while times<4:
while temp.isdigit()==False:
temp=input('说一个1到10的整数')
times=times+1
guess=int(temp)
while guess!=answer:
temp=input('猜错了 重新猜')
guess=int(temp)
times=times+1
ifguess<answer:
print('害 小了小了')
ifguess>answer:
print('还是不对 大了')
ifguess==answer:
break
print('猜对啦')
print('不过并没有奖励')
print('游戏结束不玩了88')
print('次数用完啦')
print('下次见咯')
以上是代码 想要写一个猜数游戏希望可以有3次猜的机会 但是好像这个次数没有赋值上去还是怎么样 第一个while好像就没有进行 怎么办啊哪里有问题啊{:5_104:} import random
answer=random.randint(1,10)
print('hello hello')
temp=input('不妨说说你心里想的数字')
times=1
while times<4:
while temp.isdigit() == False:
temp=input('说一个1到10的整数')
times=times+1
guess=int(temp)
ifguess<answer:
print('害 小了小了')
elifguess>answer:
print('还是不对 大了')
else:
print('猜对啦')
print('不过并没有奖励')
print('游戏结束不玩了88')
break
temp=input('猜错了 重新猜')
print('次数用完啦')
print('下次见咯') 本帖最后由 疾风怪盗 于 2020-8-30 10:51 编辑
你写的整个逻辑不通顺啊,试试看这样
import random
answer = random.randint(1, 10)
print(answer)
print('hello hello')
temp = input('不妨说说你心里想的数字')
times = 2
while times > 0:
if temp.isdigit() == False:
temp = input(f'说一个1到10的整数,还剩下{times}次机会')
times = times - 1
print(times)
else:
guess = int(temp)
times = times - 1
if guess == answer:
print('猜对啦')
print('不过并没有奖励')
print('游戏结束不玩了88')
break
else:
if guess < answer:
print('害 小了小了')
elif guess > answer:
print('还是不对 大了')
temp = input(f'猜错了 重新猜,还剩下{times + 1}次机会')
guess = int(temp)
else:
print('次数用完啦')
print('下次见咯')
baige 发表于 2020-8-30 10:22
你这样会有个bug,猜对了也会打印print('次数用完啦')
print('下次见咯') 如果你只需要猜3次,那么我觉得你的while循环甚至都可以省略了,因为你的代码中已经出现了3次input了,加入while反而没必要,而且你加入的while甚至让你的程序可以输入很多次,比如重复输入一个数字 疾风怪盗 发表于 2020-8-30 10:38
你写的整个逻辑不通顺啊,试试看这样
明白了 非常感谢 还有一点小问题 就是while后面一般是不能再用while了对吗 孙赖 发表于 2020-8-30 12:42
明白了 非常感谢 还有一点小问题 就是while后面一般是不能再用while了对吗
不是不能用,用while或者if都是从逻辑角度的吧,反正按我的思路写的,就用一次足够了
我也是初学者,我一般写的代码,基本用不到while,比较常用if,while比较难理解,会死循环。。。。。。。 疾风怪盗 发表于 2020-8-30 12:48
不是不能用,用while或者if都是从逻辑角度的吧,反正按我的思路写的,就用一次足够了
我也是初学者,我 ...
好的 非常感谢
页:
[1]