Posts: 4
Threads: 4
Joined: Jan 2020
Feb-05-2020, 03:51 PM
(This post was last modified: Feb-05-2020, 04:29 PM by Larz60+.)
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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!" )
|
_____________________________________________________________________________________________
Posts: 81
Threads: 27
Joined: Jan 2020
Feb-05-2020, 04:36 PM
(This post was last modified: Feb-05-2020, 04:37 PM by t4keheart.)
Well, assuming your indentation is all fixed up in your actual program, I would just do this a different way:
1 2 3 4 5 6 7 8 9 10 11 |
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.
Posts: 2,128
Threads: 11
Joined: May 2017
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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:
1 2 3 4 5 6 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
from string import ascii_lowercase as lowercase
def ask(question, answers, correct_answer_index, strip_whitespace = True , max_tries = None ):
incorrect_answers = 0
correct_answer_char = lowercase[correct_answer_index]
while True :
print ( f '{question}' )
for answer, char in zip (answers, lowercase):
print ( f '{char}) {answer}' )
answer = input ( 'Answer: ' )
if strip_whitespace:
answer = answer.strip()
char = char.strip()
if answer = = answers[correct_answer_index] or answer = = correct_answer_char:
print ( 'Right Answer!' )
break
else :
print ( 'Wrong Answer!' )
incorrect_answers + = 1
if max_tries is not None and max_tries < = incorrect_answers:
print ( 'Maximum limit of wrong answers are reached' )
break
return incorrect_answers
|
Then you can make a simple quiz:
1 2 3 4 5 |
fails = 0
fails + = ask( 'Where is Berlin located' , [ 'Saturn' , 'Mars' , 'Germany' ], 2 , strip_whitespace = True , max_tries = 3 )
fails + = ask( 'Where is Beijing located' , [ 'Russia' , 'China' , 'Spain' ], 1 , strip_whitespace = True , max_tries = 3 )
fails + = ask( 'What is 0xFF' , [ '16' , '255' , '32' ], 1 , strip_whitespace = True , max_tries = 3 )
print ( f 'End of game. You answered {fails} times wrong' )
|
What's missing is case insensitivity. So if you answer "china" but the right answer is "China", then it counts as wrong answer.
Task: Add case insensitivity
Posts: 8,167
Threads: 160
Joined: Sep 2016
1 2 3 4 5 6 7 8 |
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" )
|
|