0

I have done little text game and and all info of this try expect thing is some professional language what i do not understand. I am just started to studying Python so this is not easy for me.

I have lot of inputs in my game, example answer yes or no. If player writes something else, game stop working and error comes. I must do something with this. How can i use try except? Can you tell me it simply.

Piece of my game:

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')

Is there any chance to get error name input? Is there some letter or character what Python does not understand?

And if you answer something else than yes or no, how can i tell to python with try except to give question again? I hope you understand. And sorry for so long post.

1
  • Specifically what error are you getting? Please edit the question to include it Commented Oct 27, 2019 at 17:44

1 Answer 1

1

If something is causing an error, it's after the code you've shown... Using input() alone should have no reason to try-except it because anything typed is always accepted as a string.

If you want to repeat input, you'll need a loop

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")
Sign up to request clarification or add additional context in comments.

2 Comments

I mean if player writes example "ys" instead of "yes", game ask input again and let the player go when he write to input some which is needed. And then the game continues. EDIT: Sorry, now it is working. I had to stop terminal. lol. newbie mistakes.
The else block in my answer prevents that other input, and that still wouldn't have caused the code you showed to crash with an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.