Home > database >  An Algorithm That Counts How Many Answers the User Inputs Correct and Wrong Python
An Algorithm That Counts How Many Answers the User Inputs Correct and Wrong Python

Time:01-28

I have this code that will randomly generate an equation check the answer and keep going for one minute. The last thing that I want to add I a part that will check how many answers they input correct and wrong and print it at the end. I have tried to make it do that at the same time that it print if the answer was correct or wrong it would also add one to a variable but I could not get that to work. Thanks

import random 
import time 
correct=0 
wrong=0
def random_problem(num_operations):
  eq = str(random.randint(1, 100))
  for _ in range(num_operations):
    eq  = random.choice([" "])
    eq  = str(random.randint(1, 100))
  return eq 
start = time.time()
while True:
  elapsed = time.time() - start
  if elapsed > 60:
    print(correct,wrong) 
    break
  problem = random_problem(1) 
  ask=int(input(problem  ": ")) 
  solution = eval(problem)
  if ask == solution: 
    correct=correct 1,print("Correct")
  else:
    wrong=wrong 1,print("Wrong, the correct answer is",solution)

This code does not work

CodePudding user response:

In this portion of your program:

if ask == solution: 
    correct=correct 1,print("Correct")
else:
    wrong=wrong 1,print("Wrong, the correct answer is",solution)

You are trying to using the comma , to separate two different instructions, but that isn't what the comma does in Python. Instead, just place the two instructions on different lines, like this:

if ask == solution: 
    correct=correct 1
    print("Correct")
else:
    wrong=wrong 1
    print("Wrong, the correct answer is",solution)

You can have as many lines as you want inside an if, else, or any other block.

CodePudding user response:

import random 
import time 
def random_problem(num_operations):
  eq = str(random.randint(1, 100))
  one=eq
  for _ in range(num_operations):
    eq  = random.choice([" "])
    eq  = str(random.randint(1, 100))
    two=eq
    topla=int(one) int(two)
  return eq,topla
start = time.time()
while True:
  elapsed = time.time() - start
  if elapsed > 60:
     break
  topla,problem = random_problem(1) 
  ask=int(input(problem  ": ")) 
  if ask == topla: 
    print("Correct")
  else:
    print("Wrong, the correct answer is",solution)
  •  Tags:  
  • Related