Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hi how do i solve this ?
#1
Hi, right now what happens is that my codes shown below will print out error even when a wrong answer is inserted, this error message under the else is meant to only be shown when a user key in an input that is invalid, how do i make sure the error code DO NOT show even when the answer is wrong . Thank you

while True:
      print("Where is Moscow located at?")
      answer1 = input("a) Russia\nb) China\nc) Japan\nd) Mongolia\nAnswer: ")
      if answer1 == "a" or answer1 == "Russia":
            print("Right Answer!")
            break
      if answer1 == "b" or answer1 == "China":
            print("Wrong Answer!")
      if answer1 == "c" or answer1 == "Japan":
            print("Wrong Answer!")
      if answer1 == "d" or answer1 == "Mongolia":
            print("Wrong Answer!")
      else:
            print("Error!")
_____________________________________________________________________________________________
Reply
#2
Well, assuming your indentation is all fixed up in your actual program, I would just do this a different way:

while True:
    print("In which country is Moscow located?")
    answer1 = input("a) Russia\nb) China\nc) Japan\nd) Mongolia\nAnswer: ")
    if answer1 == "a" or answer1 == "Russia":
        print("Right Answer!")
        break
    elif answer1 != "a" or answer1 != "Russia":
        print('Incorrect!')
        break
    else:
        print('Error!')
Logic is, "if answer is a or china, print correct... if it's not a or china (anything else), print incorrect.
Reply
#3
Hm, the code works.
Maybe you've missed an indentation. We can't see it here, because white space are stripped away, if you don't put you code into code tags.

This works, but you can simplify this very much.
while True:
    print("Where is Moscow located at?")
    answer1 = input("a) Russia\nb) China\nc) Japan\nd) Mongolia\nAnswer: ")
    if answer1 == "a" or answer1 == "Russia":
        print("Right Answer!")
        break
    if answer1 == "b" or answer1 == "China":
        print("Wrong Answer!")
    if answer1 == "c" or answer1 == "Japan":
        print("Wrong Answer!")
    if answer1 == "d" or answer1 == "Mongolia":
        print("Wrong Answer!")
    else:
        print("Error!")
The simpler version:
while True:
    print("Where is Moscow located at?")
    answer1 = input("a) Russia\nb) China\nc) Japan\nd) Mongolia\nAnswer: ")
    if answer1 == "a" or answer1 == "Russia":
        print("Right Answer!")
        break
You can do it once right and make a function for it:
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
while True:
    print("Where is Moscow located at?")
    answer = input("a) Russia\nb) China\nc) Japan\nd) Mongolia\nAnswer: ")
    if answer.lower() in ("a", "russia"):
        print("Right Answer!")
        break
    else:
        print("wrong answer")
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020