验证不起作用

时间:2018-01-17 17:44:49

标签: python python-3.x

我正在对我的计算机科学GCSE进行控制评估,但我们没有对其进行评分,因此允许访问互联网。我们应该做3个主题测验,有3个不同的困难,每个问题5个问题。

我遇到了一个我似乎无法解决的问题。当我询问用户想要使用哪个难度或主题时,我添加了一个验证检查的东西,如果他们输入除选项之外的其他内容,重复它,但不重复,并且添加到此它始终只激活一个选项。例如,他们输入中等难度,但很容易开始测验。

守则:

check=False
while check==False:
    difficulty=input("What difficulty would you like to use?\nA-Easy\nB-Medium\nC-Hard") #\n drops down a line
    if difficulty=="a" or "easy":
        print("Easy mode turned on")
        check=True
        easy_quiz() #function written before hand
    elif difficulty=="b" or "medium":
        print("medium mode turned on")
        check=True
        medium_quiz() #function written before hand
    elif difficulty=="c" or "hard":
        print("Hard mode turned on")
        check=True
        hard_quiz() #function written before hand
    else:
        print("error, wrong input, please try again.") # Here is where i Thought it should repeat the question at the top

P.S。我是这个网站的新手,所以如果我做错了,请事先道歉

2 个答案:

答案 0 :(得分:0)

不是python专家,但您可能需要这样做 如果难度==“a”或难度==“容易”:

否则您实际可能正在测试的是 if(难度==“a”)或(“easy”不为零):

答案 1 :(得分:0)

使用" in"运算符来测试多个条件;

>>> a="hard"
>>> a in ("h", "hard")
True

>>> a = "h"
>>> a in ("h", "hard")
True

>>> a = "m"
>>> a in ("h", "hard")
False

另外(你可能会觉得这个更干净);

while True:
   if <condition 1>:
       # do something
   elif <condition 2>: 
       # do something
   elif <user requests program exit>:
       break
   else: 
       print("wrong input, blah, blah ...")
相关问题