0

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 prints 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 because it is only running the first part of the if command.

  • It would help if you posted the code that you wrote that you "could not get to work" so we can see what the problem with it is. – Andrew Merrill Jan 27 '22 at 21:53
  • In computing / scripting such as this, there is no such thing as “at the same time”. Putting a comma between statement is why it’s not working. Put the print statement after the increment and will be fine - the lines will execute within nanoseconds of each other. – S3DEV Jan 27 '22 at 21:58
  • That aside, `eval` is [considered ‘unsafe’](https://stackoverflow.com/q/1832940/6340496). Please do a bit of research as to *why* so you can ensure you’re handling it appropriately and safely. Perhaps consider the use of `ast.literal_eval` instead. – S3DEV Jan 27 '22 at 22:07

2 Answers2

1

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.

Andrew Merrill
  • 1,672
  • 11
  • 14
-1
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)
  • Care to add some context / explanation as to *why* you feel this is a viable solution; while completely ignoring the issue? – S3DEV Jan 27 '22 at 22:03