求大佬们帮我看看为什么我这个会报错啊?
import randomtimes = 3
secret = random.randint(1,10)
print("--------------------")
print("猜1到10里面猜整数哦")
guess = 0
temp = input()
if (temp is int):
while (guess != secret) and (times > 2):
guess = int(temp)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("猜大了")
else:
print("猜小了")
while guess != secret and times > 1:
print("再给你一次机会接着猜")
temp = input()
guess = int(temp)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("又猜大了")
else:
print("又猜小了")
while guess != secret and times > 0:
print("最后一次机会了啊")
temp = input()
guess = int(temp)
times = times -1
if guess == secret:
print("终于猜对了")
else:
print("废物")
print("结束了")
else:
print("1到10整数,你输的是个啥?")
temp2 = input() ///////////////////////////////就这里一直出错
if (temp2 is int):
while (guess != secret) and (times > 2):
guess = int(temp2)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("猜大了")
else:
print("猜小了")
while guess != secret and times > 1:
print("再给你一次机会接着猜")
temp2 = input()
guess = int(temp2)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("又猜大了")
else:
print("又猜小了")
while guess != secret and times > 0:
print("最后一次机会了啊")
temp2 = input()
guess = int(temp2)
times = times -1
if guess == secret:
print("终于猜对了")
else:
print("废物")
print("结束了")
else:
print("给你机会都不珍惜")
那个temp2那里一直在报错 本帖最后由 jackz007 于 2021-6-28 14:53 编辑
你确信自己写的是 Python 代码,而不是自己所发明的另一种语言?
#-*-coding:gbk-*-
import random
times = 3
secret = random . randint(1 , 10)
print("--------------------")
print("猜 1 到 10 里面猜整数哦")
while times:
while True:
print()
try:
guess = int(input("请输入一个数(1 - 10):"))# 风险语句,如果从键盘输入的不是整数,这一句就会出错,直接跳去 except 语句块继续
if guess > 0 and guess < 11: # 能正常执行到这里,说明从键盘输入的确实是一个整数
break # 跳出本级 while 循环语句块
else:
print("超出范围(1-10)!")
except: # 输入不是整数会跳来这里继续
print("无效输入!")
if guess == secret:
if times > 2:
print("厉害,一次就猜对了!")
else:
print("终于猜对了!")
break
else:
times -= 1
if times:
if guess > secret:
print("猜大了")
else:
print("猜小了")
print("再猜一次吧")
else:
print("很抱歉,机会已经用完") 缩进问题
python对t缩进非常严格
import random
times = 3
secret = random.randint(1,10)
print("--------------------")
print("猜1到10里面猜整数哦")
guess = 0
temp = input()
if (temp is int):
while (guess != secret) and (times > 2):
guess = int(temp)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("猜大了")
else:
print("猜小了")
while guess != secret and times > 1:
print("再给你一次机会接着猜")
temp = input()
guess = int(temp)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("又猜大了")
else:
print("又猜小了")
while guess != secret and times > 0:
print("最后一次机会了啊")
temp = input()
guess = int(temp)
times = times -1
if guess == secret:
print("终于猜对了")
else:
print("废物")
print("结束了")
else:
print("1到10整数,你输的是个啥?")
temp2 = input()
if (temp2 is int):
while (guess != secret) and (times > 2):
guess = int(temp2)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("猜大了")
else:
print("猜小了")
while guess != secret and times > 1:
print("再给你一次机会接着猜")
temp2 = input()
guess = int(temp2)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("又猜大了")
else:
print("又猜小了")
while guess != secret and times > 0:
print("最后一次机会了啊")
temp2 = input()
guess = int(temp2)
times = times -1
if guess == secret:
print("终于猜对了")
else:
print("废物")
print("结束了")
else:
print("给你机会都不珍惜")
另外判断是否为整数用 temp.isnumeric()而非temp is int
修改代码为
import random
times = 3
secret = random.randint(1,10)
print("--------------------")
print("猜1到10里面猜整数哦")
guess = 0
temp = input()
if temp.isnumeric():
while (guess != secret) and (times > 2):
guess = int(temp)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("猜大了")
else:
print("猜小了")
while guess != secret and times > 1:
print("再给你一次机会接着猜")
temp = input()
guess = int(temp)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("又猜大了")
else:
print("又猜小了")
while guess != secret and times > 0:
print("最后一次机会了啊")
temp = input()
guess = int(temp)
times = times -1
if guess == secret:
print("终于猜对了")
else:
print("废物")
print("结束了")
else:
print("1到10整数,你输的是个啥?")
temp2 = input()
if temp2.isnumeric():
while (guess != secret) and (times > 2):
guess = int(temp2)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("猜大了")
else:
print("猜小了")
while guess != secret and times > 1:
print("再给你一次机会接着猜")
temp2 = input()
guess = int(temp2)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("又猜大了")
else:
print("又猜小了")
while guess != secret and times > 0:
print("最后一次机会了啊")
temp2 = input()
guess = int(temp2)
times = times -1
if guess == secret:
print("终于猜对了")
else:
print("废物")
print("给你机会都不珍惜")
print("结束了")
能把程序学这么长太厉害了。晕倒 jackz007 发表于 2021-6-28 14:43
你确信自己写的是 Python 代码,而不是自己所发明的另一种语言?
谢谢大佬了,虽然有些东西看不太懂。。。我是按照小甲鱼的从零开始学python那个课写的代码,刚学到第四课。还没学到风险语句那里try该怎么用。。。所以我用了if temp is int没想到没报错。然后后面我就接着这么写了。 逃兵 发表于 2021-6-28 14:47
缩进问题
python对t缩进非常严格
谢谢大佬了!那个if是我自作聪明瞎写的,发现没报错还以为能这么用、太谢谢了
页:
[1]