python里isalpha函数_python3:尝试检查isalpha函数,但不工作

你有点问题。在

首先,不要将输入转换为str。它已经作为字符串从input传入。在

第二,你永远不会用你想要捕捉的异常来引发异常,因为你的输入在try/except之外。不仅如此,如果输入abcd1234之类的内容,也不会引发异常。这仍然是一个有效的字符串。在

你有奖金问题。千万不要抓住打开的Exception。总是明确你想要捕捉什么样的异常。不过,你不需要尝试一下。相反,只需检查您是否有有效的条目并继续您的逻辑。在

只需删除try/except甚至isalnum检查,只需检查输入的字符串是否与您要查找的字符串匹配。如果没有,则输出某种错误消息:def bear_room():

print("there's a bear here")

print("the bear has a bunch of honey")

print("the fat bear is front of another door")

print("how are you going to move the bear?")

choice = input("(Taunt bear, take honey, open door?: ")

if choice == "take honey":

print("the bear looks at you then slaps your face off")

elif choice == "open door":

print("get the hell out")

else:

print("Invalid entry")

bear_room()

bear_room()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值