我做过一些小型的文字游戏,并且所有此尝试的信息都希望我不理解某些专业语言。我才刚刚开始学习Python,所以这对我来说并不容易。
我的游戏中有很多输入,示例回答是或否。如果玩家写了别的东西,游戏将停止工作并出现错误。我必须为此做些事情。我如何使用try除外?你能简单告诉我吗。
我的游戏片段:
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?我希望你明白。很抱歉,发布了这么长时间。
答案 0 :(得分:1)
如果某些原因导致错误,则在显示代码之后...仅使用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")