1

I'm making a simple text game in Python 3, and I tried to make an input statement, but when I answer it, the program closes. How can I fix this?

I've tried Googling it, but I just see "program closes straight after opening it".

def startgame():
    print("####################")
    ign = input("Type a name. ")
    print("####################")
    namedecision = input("You have picked " + ign + ".\nAre you sure you want\nto keep this name? (Y/N) ")
    if namedecision == ['y','Y']:
        # put game func
        print("####################")
    if namedecision == ['n', 'N']:
        ign = input("Type a name. ")
        namedecision = input("You have picked " + ign + ".")
        print("####################")
        # game func

startgame()

I expect that if I typed Y or N it would execute the proper functions.

3
  • is your program really indented like this? If so, you need to correct that. Commented Aug 5, 2019 at 19:44
  • Are you running it from the shell? Commented Aug 5, 2019 at 19:44
  • No, it's actually indented properly. Do you mean the interpreter? Commented Aug 5, 2019 at 19:46

1 Answer 1

2

Your if statements are always returning false because your input will always be a string, and you are comparing it to a list. Change your == for in in order to check if a string (or any other type) is in a list.

Corrected code:

def startgame():
    print("####################")
    ign = input("Type a name. ")
    print("####################")
    namedecision = input("You have picked " + ign + ".\nAre you sure you want\nto keep this name? (Y/N) ")
    if nameDecision in ['y','Y']:
        # put game func
        print("####################")
    elif nameDecision in ['n', 'N']:
        ign = input("Type a name. ")
        namedecision = input("You have picked " + ign + ".")
        print("####################")

startgame()
Sign up to request clarification or add additional context in comments.

1 Comment

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.