Python尝试除输入

Python尝试除输入,python,Python,我做了一个小的文字游戏,所有关于这件事的信息都是一些我不懂的专业语言。我刚刚开始学习Python,所以这对我来说并不容易 我在游戏中有很多输入,例如回答“是”或“否”。如果玩家写了其他东西,游戏停止工作,错误就会出现。我必须做点什么。如何使用try-except?你能简单地告诉我吗 我的游戏片段: print('Hello you! Welcome to the Ghetto! What is first name?') first_name=input() print('Hello',fir

我做了一个小的文字游戏,所有关于这件事的信息都是一些我不懂的专业语言。我刚刚开始学习Python,所以这对我来说并不容易

我在游戏中有很多输入,例如回答“是”或“否”。如果玩家写了其他东西,游戏停止工作,错误就会出现。我必须做点什么。如何使用try-except?你能简单地告诉我吗

我的游戏片段:

print('Hello you! Welcome to the Ghetto! What is first name?')
first_name=input()

print('Hello',first_name+'! Now tell me your last name also!')
last_name=input()

print('You are my bailout',first_name,last_name+'! I really need your help here. I lost my wallet last night when i was drunk.') 
print('The problem is i dont wanna go alone there to get it back cause this place is so dangerous. Can you help me? Answer "YES" or "NO"')

lista1 = ['yes','y']
lista2 = ['no','n']

answer1=input().lower()

if answer1 in lista2:
    print('If you are not interested to help me with this problem, go away from here and never come back!!')

    print('*** Game end ***')

if answer1 in lista1:
    print('That is awesome man! You can see that big house huh? There is three apartments and my wallet is in one of them.')

    print('But you have to be god damn careful there you know, these areas is not safe to move especially you are alone. So lets go my friend')
是否有可能获得错误名称输入?是否有Python不理解的字母或字符


若你们回答的不是“是”或“否”,那个么除了再次提问,我如何告诉python“尝试”呢?我希望你能理解。很抱歉发布了这么长的帖子。

如果有什么原因导致错误,则是在您显示的代码之后。。。单独使用input()应该没有理由尝试,除了它,因为键入的任何内容都始终被接受为字符串

如果你想重复输入,你需要一个循环

while True:
    answer1=input().lower()
    if answer1 in ["yes", "y"]:
        print("ok") 
        break 
    elif answer1 in ["no", "n"]:
        print("end")
        break
    else:
        print("try again")

具体地说,您得到了什么错误?请编辑问题以包括itI意思是如果玩家写示例“ys”而不是“是”,游戏再次询问输入,并让玩家在他写输入时离开。然后游戏继续。编辑:对不起,现在它正在工作。我不得不停下来。lol。新手出错。我的答案中的else块阻止了其他输入,这仍然不会导致您显示的代码因错误而崩溃