Home > Software engineering >  Why wont this code work properly its doing more than 20 question and it is only doing the easy level
Why wont this code work properly its doing more than 20 question and it is only doing the easy level

Time:08-29

from random import randint

name = input("What's your name? ")

print(f'Welcome to this maths quiz {name} you will get ten question with addition, 
subtraction, multiplication and division, you will get ten seconds to answer good luck')


selectedQuiz = input("Please enter a difficulty: Easy, Medium, Hard ")


num_right = 0
num_wrong = 0


playing = True

if(selectedQuiz == "Easy" or "easy"):
while playing == True:
    for i in range(0,20):
        num1 = randint(2,9)
        num2 = randint(2,9)
        product = num1 * num2
    
        response = input(f'What is {num1} * {num2}? ')
        if int(response) == product:
            num_right  = 1
            print('Correct')
        else:
            num_wrong  = 1
            print('Incorrect')   

    
print(f'You got {num_right} right out of 20.')

elif(selectedQuiz == "Medium" or"medium"):
while(playing == True):
    for i in range(0,20):
        num1 = randint(10,15)
        num2 = randint(10,15)
        product = num1 * num2
    
        response = input(f'What is {num1} * {num2}? ')
        if int(response) == product:
            num_right  = 1
            print('Correct')
        else:
            num_wrong  = 1
            print('Incorrect') 
    print(f'You got {num_right} right out of 20.')

elif(selectedQuiz == "Hard" or"hard"):
while(playing == True):
    for i in range(0,20):
        num1 = randint(15,20)
        num2 = randint(15,20)

        product = num1 * num2
    
        response = input(f'What is {num1} * {num2}? ')
        if int(response) == product:
            num_right  = 1
            print('Correct')
        else:
            num_wrong  = 1
            print('Incorrect') 
        print(f'You got {num_right} right out of 20.')
else:
    print("please enter a valid quiz type")

When I type hard and medium it is still doing the easy mode and the quiz is doing more than 20 question even though it should stop after 20 and give you your result for the quiz. What way can I fix this problem and find the solution. I currently cant find a good solution to this. Thanks.

CodePudding user response:

  1. it's always selecting easy mode because your expression selectedQuiz == "Easy" or "easy" is equivalent to (selectedQuiz == "Easy") or "easy", so you are asking python is selectedQuiz "Easy" if False return "easy" and strings with length greater than 0 evalute to True. what you should do instead is selectedQuiz == "Easy" or selectedQuiz == "easy".
  2. The quiz is never ending because you have while playing and playing is always True, so what you need to do is set it to false somewhere (maybe after the for loop that counts to 20)

CodePudding user response:

Issue 1: Always going into easy / the first if statement.

I believe the issue is with the way you are constructing your boolean statement.

If you want to compare selectedQuiz to the strings "Easy" or "easy", you have to construct 2 true or false statements that are then compared with or.

By adding selectedQuiz == after the or, you are creating 2 correct boolean statements that could then be compared with or

Solution For Issue 1: if (selectedQuiz == "Easy" or selectedQuiz == "easy" )

Issue 2: The program runs more than 20 times.

You might notice that your app runs in increments of 20 and never stops. It never stops because you are looking for the variable "playing" to be false for a break but you never set it to false. Instead, it finishes the for loop with a range of 20 and then restarts the for loop.

Solution For Issue 2: Add playing = false at the end of your for loop.

You can probably optimize your code by always lowercasing the input before comparing so that you only have one boolean statement for the "easy", "medium", and "hard" if statements. You might also want to see if you need those while loop. If you don't need continuous increments of 20 you might only need the for loop and then use "break" to break out of the for loop or "continue" to skip loops in the for loop. Food for thought.

CodePudding user response:

You can make the level selection more efficient by adding .lower() in the end of the input function and set the condition to if selectedQuiz == 'hard':

The code gave you more than 20 questions because you never to told the code when to stop, this can be fixed by adding playing = False after the for loop ends.

Full Code:

from random import randint

name = input("What's your name? ")

print(f"Welcome to this maths quiz {name} you will get ten question with addition, subtraction, multiplication and division, you will get ten seconds to answer good luck")


selectedQuiz = input("Please enter a difficulty: Easy, Medium, Hard ").lower()


num_right = 0
num_wrong = 0

playing = True

while playing:
   if selectedQuiz == "easy":
      for i in range(0,20):
         num1 = randint(2,9)
         num2 = randint(2,9)
         product = num1 * num2
      
         response = input(f'What is {num1} * {num2}? ')
         if int(response) == product:
            num_right  = 1
            print('Correct')
         else:
            num_wrong  = 1
            print('Incorrect')
      
      print(f'You got {num_right} right out of 20.')
      playing = False

   elif selectedQuiz == "medium":
      for i in range(0,20):
         num1 = randint(10,15)
         num2 = randint(10,15)
         product = num1 * num2
      
         response = input(f'What is {num1} * {num2}? ')
         if int(response) == product:
               num_right  = 1
               print('Correct')
         else:
               num_wrong  = 1
               print('Incorrect') 
      
      print(f'You got {num_right} right out of 20.')
      playing = False

   elif selectedQuiz == "hard":
      for i in range(0,20):
         num1 = randint(15,20)
         num2 = randint(15,20)

         product = num1 * num2
      
         response = input(f'What is {num1} * {num2}? ')
         if int(response) == product:
               num_right  = 1
               print('Correct')
         else:
               num_wrong  = 1
               print('Incorrect') 
      print(f'You got {num_right} right out of 20.')
      playing = False
   else:
      print("please enter a valid quiz type")
      break
  • Related