0
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.

Here i have my code for a school project however the != function I used at the start of the to make sure the system is stopped if any number that isn't 1 or 2 as if I enter any number even one or two the program just stops and the != function hasn't been working properly when I have tried to be using it in some of my other programs as well.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ibrahim
  • 7
  • 5
  • FYI -- the code for a question like this should be, perhaps, two lines: One to set `level_of_difficulty = 1`, and another to run `if level_of_difficulty != 1 or level_of_difficulty != 2: print "This should not happen"`, or such. See http://stackoverflow.com/help/mcve/ for hints on creating a **minimal**, complete, verifiable example as required by site rules. – Charles Duffy Jul 23 '15 at 18:31

1 Answers1

0

This will always evaluate to True.

if level_of_difficulty != 1  or 2:

It is equivalent to

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

What you need is

if level_of_difficulty != 1 and level_of_difficulty != 2:

Here is a shorter way to write this:

if level_of_difficulty not in (1, 2):
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
  • Thanks I understand but in what context could I be able to use the != function – Ibrahim Jul 23 '15 at 16:52
  • I think he's got something else going on here. Even when I try to run like that, it kills the program. But the answer is entirely correct, the evaluation needs to change. – Sandwich Heat Jul 23 '15 at 16:55
  • You are trying to compare a single value against multiple values. The not-equal operator is for testing a single value against a single value. So if you are comparing a single value against two values you need to use the not-equal operator for each comparison. – Steven Rumbalski Jul 23 '15 at 16:55
  • `level_of_difficulty != 1 or level_of_difficulty != 2` will always be true, because if `level_of_difficulty == 1`, then `level_of_difficulty != 2`, and the inverse. Thus, this is **not** equivalent to `if level_of_difficulty not in (1, 2)`. – Charles Duffy Jul 23 '15 at 16:56
  • @CharlesDuffy: Yeah, already caught that and corrected it. – Steven Rumbalski Jul 23 '15 at 16:57