-2

I am creating a quiz using a list and I want to make it so that question_prompts[3] accepts the answer ".58" and "0.58" and question_prompts[6] accepts the answer "f" and "F". But it isn't working. does anyone how how to fix this

from question import question
import math
print("Hello my name if jack rosenblatt and I will be asking you a few simple questions today.\n")
print('Directions: All answers requiring decimal notation should be rounded to two decimal-point numbers\
(e.g., 2.01, not 2.014. \n\nPlease Enter your First and Last name below.\n')
firstName = input('First Name:')
lastName= input('Last Name:')
print('\n')
question_prompts= ["1. What is the value of π?\n ",
"2. What is the value of φ? \n",
"3. What is the value of e, the Euler number?\n ",
"4. What is the value of C, the Euler constant?\n ",
"5. What is the largest digit in Base 2? \n",
"6. What is the largest digit in Base 8? \n",
"7. What is the largest digit in Base 16?\n",
"8. What is the value of the multiplier effect in economics?\n ",
"9. If 100 is reduced by 10%, what is the result?\n ",
"10. If the result of question 9 is increased by 10%, what is the new result?\n"]
correct_answers=["3.14","1.61","2.72","0.58","1","7","f","2.50","90","99"]
questions= [question(question_prompts[0],"3.14"),
            question(question_prompts[1],"1.61"),
            question(question_prompts[2],"2.72"),
            question(question_prompts[3],"0.58"),
            question(question_prompts[4],"1"),
            question(question_prompts[5],"7"),
            question(question_prompts[6],"F"),
            question(question_prompts[7],"2.50"),
            question(question_prompts[8],"90"),
            question(question_prompts[9],"99")]
def run_test(questions):
    score = 0 
    for question in questions:
        answer = input(question.prompt)
        score +=1
        if answer == question.answer:
            print('Congrats ' + firstName + ' that is correct. Lets move on to the next question.\n')
        elif answer !=question.answer:
            print('Sorry ' + firstName + ' that was wrong you should have gotten')

            if question_prompts[3] == ".58" or "0.58":
                print('Congrats that is correct. Lets move on to the next question.\n')

            elif question_prompts[6] == "F" or "f":
                print('Congrats that is correct. Lets move on to the next question.\n')
            else:
                print('Sorry ' + firstName + ' that was wrong you should have gotten')

        else:
            print('Sorry ' + firstName + ' that was wrong you should have gotten')
run_test(questions)
```
  • You can strip leading zeros from all answers and inputs and lowercase all questions and answers. Then in your function you can format it back to how you want it to look like. – Edeki Okoh May 06 '19 at 17:33

1 Answers1

1

question_prompts[3] == ".58" or "0.58" will evaluate to True always. The reason for this is "0.58" is a non-empty string, and therefore will evaluate as True. What you were looking for was question_prompts[3] == ".58" or question_prompts[3] == "0.58"

Instead of this, I would suggest converting the value to float so that you can compare the numerical representation rather than the string. Because what happens if someone enteres .580? In your case you would have to add a condition for each possible string. If you do float(question_prompts[3]) == .58 instead, then you cover multiple scenarios.

The same problem occurs with f, you need to specify question_prompts[6] == "F" or question_prompts[6] == "f". Once again a better solution for you may be to do this: question_prompts[6].lower() == 'f'. This just converts it to lowercase and then you only need one comparison.

Chrispresso
  • 3,660
  • 2
  • 19
  • 31