关于except语句的执行问题
import randomsecret = random.randint(1,10)
times = 3
print("games")
print("输入数字",end = "")
guess = 0
while times != 0 and guess != secret:
try:
temp = input()
guess = int(temp)
times = times - 1
if guess == secret:
print("yes")
else:
if guess > secret:
print("big")
print("again",end = "")
else:
print("small")
print("again",end = "")
except ValueError as reason:
print('哈哈出错了,值错误',str(reason))
except (EOFError,KeyboardInterrupt) as reason:
print('出错了',str(reason))
print("end")
print('测试,我被执行了') #这个没有执行
#-------------------------------------------------------------------------------------------------------------
import random
secret = random.randint(1,10)
print('------------------我爱鱼C工作室------------------')
try:
temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
guess = int(temp)
except (ValueError, EOFError, KeyboardInterrupt):
print('输入错误!')
guess = secret
while guess != secret:
temp = input("哎呀,猜错了,请重新输入吧:")
guess = int(temp)
if guess == secret:
print("我草,你是小甲鱼心里的蛔虫吗?!")
print("哼,猜中了也没有奖励!")
else:
if guess > secret:
print("哥,大了大了~~~")
else:
print("嘿,小了,小了~~~")
print("游戏结束,不玩啦^_^")
print('测试,我被执行了') #这个执行了
这两段代码我实在不能理解,为什么上面代码红色部分没有被执行,下面代码红色部分被执行了?上面代码try在while里面 while之后不应该执行print吗?
我这里运行就被执行了啊,这个理论上也会被执行的,可能是你那的问题吧。
页:
[1]