我正在尝试让这个问题接受两种形式的答案,但它不起作用

时间:2019-05-06 17:28:02

标签: python python-3.x

我正在使用列表创建测验,因此我想使question_prompts [3]接受答案“ .58”和“ 0.58”,而question_prompts [6]接受答案“ f”和“ F”。但这不起作用。有谁怎么解决这个问题

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)
```

1 个答案:

答案 0 :(得分:1)

question_prompts[3] == ".58" or "0.58"将始终评估为True。原因是"0.58"是一个非空字符串,因此将评估为True。您正在寻找的是question_prompts[3] == ".58" or question_prompts[3] == "0.58"

相反,我建议将值转换为float,以便您可以比较数字表示形式而不是字符串。因为如果有人输入.580会发生什么?在您的情况下,您必须为每个可能的字符串添加一个条件。如果改为使用float(question_prompts[3]) == .58,那么您将涵盖多种情况。

f也会发生相同的问题,您需要指定question_prompts[6] == "F" or question_prompts[6] == "f"。再一次为您提供更好的解决方案是:question_prompts[6].lower() == 'f'。这只是将其转换为小写,然后您只需要一个比较。