首页 > 编程知识 正文

python中的isalpha,python isalpha函数用法

时间:2023-05-05 06:22:09 阅读:273422 作者:92

你有点问题。在

首先,不要将输入转换为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()

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。