输入小数为什么会一直循环
import randomsecret = random.randint(1,10)
game=input("guess a number from 0~10 :")
n=1
while n!=0:
if game.isdigit():
guess=int(game)
if guess == secret:
print("wow,you can really dance!")
while guess !=secret and n < 4:
game=input("try again:")
guess=int(game)
if guess == secret:
print("wow,you can really dance!")
if guess > secret:
print("nonono,too loud!")
n=n+1
if guess < secret:
print("you should put more in")
n=n+1
else:
print("抱歉,您的输入有误,请输入一个整数:", end='')
print("game over...") 本帖最后由 青出于蓝 于 2021-7-15 21:39 编辑
import random
secret = random.randint(1,10)
game=input("guess a number from 0~10 :")
n=1
while n!=0:
if game.isdigit():
guess=int(game)
if guess == secret:
print("wow,you can really dance!")
while guess !=secret and n < 4:
game=input("try again:")
guess=int(game)
if guess == secret:
print("wow,you can really dance!")
if guess > secret:
print("nonono,too loud!")
n=n+1
if guess < secret:
print("you should put more in")
n=n+1
else:
print("抱歉,您的输入有误,请输入一个整数:", end='')
break
print("game over...")
输入错误也要break停止循环
缩进好像有些问题,楼主自己调整吧 青出于蓝 发表于 2021-7-15 21:37
输入错误也要break停止循环
缩进好像有些问题,楼主自己调整吧
import random
times = 3
secret = random.randint(1,10)
print('------------------我爱鱼C工作室------------------')
guess = 0
print("不妨猜一下小甲鱼现在心里想的是哪个数字:", end=" ")
while (guess != secret) and (times > 0):
temp = input()
if temp.isdigit():
guess = int(temp)
if guess == secret:
print("我草,你是小甲鱼心里的蛔虫吗?!")
print("哼,猜中了也没有奖励!")
else:
if guess > secret:
print("哥,大了大了~~~")
else:
print("嘿,小了,小了~~~")
if times > 1:
print("再试一次吧:", end='')
else:
print("机会用光咯T_T")
else:
print("抱歉,您的输入有误,请输入一个整数:", end='')
times = times - 1 # 用户每输入一次,可用机会就-1
print("游戏结束,不玩啦^_^")
为什么小甲鱼的答案不会一直循环呢?麻烦告知一下啦。。。 青出于蓝 发表于 2021-7-15 21:37
输入错误也要break停止循环
缩进好像有些问题,楼主自己调整吧
而且break了就直接gameover了呀?是缩进的问题吗我再看看 XIMO衿霄 发表于 2021-7-15 21:40
import random
times = 3
判断完后times会统一减1,当然就不会一直循环了 本帖最后由 青出于蓝 于 2021-7-15 21:47 编辑
import random
secret = random.randint(1,10)
game=input("guess a number from 0~10 :")
n=1
while n!=0:
if game.isdigit():
guess=int(game)
if guess == secret:
print("wow,you can really dance!")
while guess !=secret and n < 4:
game=input("try again:")
guess=int(game)
if guess == secret:
print("wow,you can really dance!")
if guess > secret:
print("nonono,too loud!")
n=n+1
if guess < secret:
print("you should put more in")
n=n+1
else:
game=input("抱歉,您的输入有误,请输入一个整数:", end='')
print("game over...")
青出于蓝 发表于 2021-7-15 21:43
判断完后times会统一减1,当然就不会一直循环了
哦对了!
缩进是我按了Tap它自动缩的,应该没问题。但加了break输入小数后就直接终止了。。。我还是推翻重来吧。。。
页:
[1]