|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random
secret = random.randint(1,10)
chance = 3
guess = 0
print("猜猜小甲鱼心里的数字:",end='')
while chance > 0 and guess != secret:
temp = input()
if temp.isdigit():
guess = int(temp)
if guess == secret:
print("我草,你是小甲鱼心里的蛔虫吗?")
print("哼,猜中了也没奖励")
else:
if guess > secret:
print("大了大了")
else:
print("小了小了")
if chance > 1:
print("请再试一次吧:",end='')
else:
print("机会用光咯")
chance = chance - 1
elif guess <= 0 or guess > 10 or guess == float:
print("输入有误,请输入1-10之间的整数")
else:
print("抱歉,您的输入有误,请输入一个整数:",end='')
print("游戏结束,不玩啦")
这是萌新我尝试改进这个程序写的代码,问题出在倒数第5行那里,请问一下为什么guess<=0 or guess>10这个部分运行的时候没有效果而后面的guess==float又可以执行??求大神讲解一波,教下我如何改进!!谢谢!
当你输入100时,在if temp.isdigit()的时候就为True了,所以执行不到后面的elif
guess==float是不可行的,但是,当你输入5.4时,因为满足guess <= 0,而你用的是or,所以条件成立
- import random
- secret = random.randint(1,10)
- chance = 3
- guess = 0
- print("猜猜小甲鱼心里的数字:",end='')
- while chance > 0 and guess != secret:
- temp = input()
- if temp.isdigit():
- if int(temp) > 10 or int(temp) == 0:
- print("输入有误,请输入1-10之间的整数")
- continue
- else:
- if temp[0] == "-" and temp[1:].isdigit():
- print("输入有误,请输入1-10之间的整数")
- continue
- elif "." in temp and (temp[1].isdigit() or temp[0].isdigit()):
- print("输入有误,请输入1-10之间的整数")
- continue
- else:
- print("抱歉,您的输入有误,请输入一个整数:",end='')
- continue
- if temp.isdigit():
- guess = int(temp)
- if guess == secret:
- print("我草,你是小甲鱼心里的蛔虫吗?")
- print("哼,猜中了也没奖励")
- else:
- if guess > secret:
- print("大了大了")
- else:
- print("小了小了")
- if chance > 1:
- print("请再试一次吧:",end='')
- else:
- print("机会用光咯")
- chance = chance - 1
- print("游戏结束,不玩啦")
复制代码
|
|