python输入q结束程序_试图让一个Python程序以字母“q”退出,但是输入是一个整数?...

下面的代码片段。我试着搞乱another answer posted on here,但似乎一点也不管用。我不知道我做错了什么。在Xubuntu 18.04 LTS上使用Python3。代码如下:while True:

try:

print("Your room is DARK, yet a light flashes red. What do you do?")

print("")

print("1. Look around.")

print("2. There's a lamp somewhere...")

print("3. Go back to bed.")

print("")

ans = int(input(">>> "))

if ans == 1:

print("")

print("Too dark to see... better find a light...")

time.sleep(2)

if ans == 2:

print("")

print("Fumbling, you turn on your nightstand lamp...")

break

if ans == 3:

print("")

print("You sleep away the troubles... but you can't stay asleep...")

time.sleep(1)

print("")

print("Back to the world of the living...")

if ans == str('q'):

sys.exit(0)

except ValueError:

print("")

所以,当用户输入“q”时,我希望程序关闭。我好像一点也做不到。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值