0

I have this python source code:

while True:
    answer = input("\nApple or Nokia?")
    if answer == 'Apple':
        print("Nice, you have Apple!")

    if answer == 'Nokia':
        print("Nice, you have Nokia!")

    else:
        print("I do not understand.")

But when I run it and write Apple, the python says:

Apple or Nokia?Apple
Nice, you have Apple!
I do not understand.

Apple or Nokia?

I don't know why python writes "I do not understand."

2
  • 1
    elif answer == 'Nokia': Commented Apr 7, 2021 at 10:36
  • 1
    the second if should be an elif (else if). Otherwise on an 'Apple' input the first if and the else from the second if will be executed. Commented Apr 7, 2021 at 10:36

1 Answer 1

1

You have typo:

if answer == 'Nokia':

should be

elif answer == 'Nokia':

your original code is equivalent to

while True:
    answer = input("\nApple or Nokia?")
    if answer == 'Apple':
        print("Nice, you have Apple!")
    else:
        pass  # do nothing

    if answer == 'Nokia':
        print("Nice, you have Nokia!")

    else:
        print("I do not understand.")
Sign up to request clarification or add additional context in comments.

Comments

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.