我是否正确编程,因为某些原因!=功能不起作用

时间:2015-07-23 16:42:08

标签: python

import sys# The sys function is imported from the python library so that the quiz can be programmed to stop at any point during the
# quiz.
import random# The random function is imported from the python library to enable the program to use different numbers easily and
# more efficiently.
name=input("What is your name?")
print ("Alright",name,"welcome to your maths quiz")# Here the code is programmed to ask for the student's name and prints this
# statement to help engage them.
score=0
# Here students are given the option to select a level of difficulty of their choice.
level_of_difficulty = int(input(("What level of difficulty are you working at?\n"
                             "Press 1 for low, 2 for intermediate "
                             "or 3 for high\n")))

if level_of_difficulty != 1  or 2:# If the user doesn't enter any of these numbers the program stops and the user has to start
# again.
    sys.exit()# The program uses the sys function imported at the start of the program to do this.

if level_of_difficulty == 3: # These if and else statements can be removed or altered by teachers depending on how they would like
# to use the different operations in each level.
    ops = ['+', '-', '*', '/']
else:
    ops = ['+', '-', '*']

for question_num in range(1, 11):# Here the figure 11 can be changed by teachers to any number of their choosing if they would like
# to change the amount of questions in the quiz.
    if level_of_difficulty == 1:
        number_1 = random.randrange(1, 10)# These randrange values can be altered by teachers to make questions more challenging or
        number_2 = random.randrange(1, 10)# easier for students depending on their needs.
    else:
        number_1 = random.randrange(1, 20)# The use of the else function here enables there to be a different range of numbers for
        number_2 = random.randrange(1, 20)# the different levels of difficulty.

    operation = random.choice(ops)# Here the use of the random.choice function ensures that a random operation is used for each
# question.
    maths = round(eval(str(number_1) + operation + str(number_2)),5)# Here the evaluation function ensures that the variable
# operation is not recognised as a string but as an operation and the round function ensures that any answer is rounded and is
# correct to within 5 decimal places.
    print('\nQuestion number: {}'.format(question_num))# Here the format function ensures that the correct statement is printed for
# each question.
    print ("The question is",number_1,operation,number_2)

    answer = float(input("What is your answer: "))
    if answer == maths:# This use of selection means that the students score only increases when the correct annswer is entered and
        print("Correct")# a matching statement is also printed i.e. "correct" is printed when the correct answer is submitted.
        score = score + 1
    else:
        print ("Incorrect. The actual answer is",maths)# Here a correcting statement is printed if the wrong answer has been
# submitted. Teachers have the option to change these matching statements if they so wish.

if score >5:
# Here the program is coded to print the student score at the end of the quiz and a matching is statement printed
# to go with.
    print("Well done you scored",score,"out of 10")
else:
    print("Unfortunately you only scored",score,"out of 10. Better luck next time")
# Again these statements can be altered to match the teachers needs.

这里我有一个学校项目的代码,但是我在开始时使用的!=函数,以确保系统停止,如果任何数字不是1或2,就好像我输入任何数字一样当我试图在我的其他一些程序中使用它时,一个或两个程序停止并且!=函数还没有正常工作。

1 个答案:

答案 0 :(得分:0)

这将始终评估为True

if level_of_difficulty != 1  or 2:

相当于

if (level_of_difficulty != 1)  or bool(2): # bool(2) is always True

您需要的是

if level_of_difficulty != 1 and level_of_difficulty != 2:

这是一个较短的写法:

if level_of_difficulty not in (1, 2):