-1
user_grade=0
users_name=input("What is your name \n")
import random

count=1
while count<=10:
  ops = ["+" , "-", "*", "/"]
  num1 = random.randint(0,50)
  num2 = random.randint(0,20)
  operation = random.choice(ops)


  math=(num1, operation, num2)
  user_answer=int(input("What is " + str(math) + "\n"))
  if user_answer==num1 + num2:
    print("Correct answer")
    user_grade+=1

  if user_answer==num1 - num2:
    print("Correct answer")
    user_grade+=1

  if user_answer==num1 * num2:
    print("Correct answer")
    user_grade+=1

  if user_answer==num1 / num2:
    print("Correct answer")
    user_grade+=1
  else:
    print("incorrect or invalid")
  count+=1

print("You got ", user_grade ,"/10")

this the output:

What is your name 
Ibad
What is (39, '*', 2)
78
Correct answer
incorrect or invalid

why does the incorrect part keep on repeating

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Trenton McKinney Aug 05 '20 at 04:05

2 Answers2

1

You're using if when you're supposed to be using elif. elif creates a block of statements, exiting upon the first match or True. Using separate if statements will check all of the conditions, which will lead to error even when there is none.

user_grade=0
users_name=input("What is your name \n")
import random

count=1
while count<=10:
  ops = ["+" , "-", "*", "/"]
  num1 = random.randint(0,50)
  num2 = random.randint(0,20)
  operation = random.choice(ops)


  math=(num1, operation, num2)
  user_answer=int(input("What is " + str(math) + "\n"))
  if user_answer==num1 + num2:
    print("Correct answer")
    user_grade+=1

  elif user_answer==num1 - num2:
    print("Correct answer")
    user_grade+=1

  elif user_answer==num1 * num2:
    print("Correct answer")
    user_grade+=1

  elif user_answer==num1 / num2:
    print("Correct answer")
    user_grade+=1
  else:
    print("incorrect or invalid")
  count+=1

print("You got ", user_grade ,"/10")
s d
  • 584
  • 1
  • 4
  • 16
0

use or operator instead of that many if's

if((user_answer==num1 + num2) or (user_answer==num1 - num2) or (user_answer==num1 * num2) or (user_answer==num1 / num2)):
   user_grade+=1
else:
   print('Incorrect or invalid)
Ahmet
  • 7,527
  • 3
  • 23
  • 47