hi My code as shown below, when i am trying to execute this code the result always gives me the else clause output, not able to understand where part of code is not working can you help me with it. Even if i type the correct answer still the out put is of else clause
quizz = {
'Question1':{
'question':'what is the capital of India ',
'answer':'Delhi\n'
},
'Question2':{
'question':'what is the capital of germany ',
'answer':'Berlin\n'
}
}
score = 0
for key,value in quizz.items():
print(value['question'])
answer = input('Enter your answer ')
if answer.lower() == value['answer'].lower():
print('Thats correct answer')
score = 1
print('your score is ' str(score))
else:
print('wrong')
print('your score is: ' str(score) )
when the user input matches with the answer it should give me the proper output
CodePudding user response:
input()
returns the result without the trailing '\n'
. Just remove it from the answers in quizz
, and it will work.
CodePudding user response:
quizz = {
'Question1':{
'question':'what is the capital of India ',
'answer':'Delhi\n'
},
'Question2':{
'question':'what is the capital of germany ',
'answer':'Berlin\n'
}
Because of the \n null character in the aswers, the logic sees it as unequal in the comparison. When you remove them, the problem will be solved.
CodePudding user response:
this code will work, your mistake was using '\n' in the string, i'm assuming you meant for it to create a new line - you don't need to include it :)
quizz = {
'Question1':{
'question':'what is the capital of India? ',
'answer':'Delhi'
},
'Question2':{
'question':'what is the capital of Germany? ',
'answer':'Berlin'
}
}
score = 0
for key,value in quizz.items():
print(value['question'])
answer = input('Enter your answer: ')
if answer.lower() == value['answer'].lower():
print('That is the correct answer')
score = 1
print('your score is ' str(score))
else:
print('that is the incorrect answer...')
print('your score is: ' str(score) )
CodePudding user response:
You Have To Remove \n At Answer
So The Code Will Be:
quizz = {
'Question1':{
'question':'what is the capital of India ',
'answer':'Delhi'
},
'Question2':{
'question':'whenter code hereat is the capital of germany ',
'answer':'Berlin'
}
}
score = 0
for key,value in quizz.items():
print(value['question'])
answer = input('Enter your answer ')
if answer.lower() == value['answer'].lower():
print('Thats correct answer')
score = 1
print('your score is ' str(score))
else:
print('wrong')
print('your score is: ' str(score) )