|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
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)
while guess!=answer:
temp=input('猜错了 重新猜')
guess=int(temp)
times=times+1
if guess<answer:
print('害 小了小了')
if guess>answer:
print('还是不对 大了')
if guess==answer:
break
print('猜对啦')
print('不过并没有奖励')
print('游戏结束不玩了88')
print('次数用完啦')
print('下次见咯')
以上是代码 想要写一个猜数游戏希望可以有3次猜的机会 但是好像这个次数没有赋值上去还是怎么样 第一个while好像就没有进行 怎么办啊哪里有问题啊
本帖最后由 疾风怪盗 于 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('下次见咯')
复制代码
|
|