Syntax Error
I am trying to make a program that asks you division questions with Python.
I tried using the code:
Imports the libraries we need
import random
Setup
num1 = random.randint(1, 100)
num2 = random.randint(1, 100)
TrueAnswer = num1/num2
print("How many problems do you want?")
Number_Of_Problems = int(input(""))
def AskProblem():
for i in range(1, Number_Of_Problems):
if num2 > num1:
AskProblem()
else:
# Asks the division question
print(f"What is {num1}÷{num2}?")
MyAnswer = int(input(""))
# Checks the answer
if MyAnswer == TrueAnswer:
print("Correct!")
else:
print("Incorrect!")
AskProblem()
if "?" in MyAnswer:
print("Do you need any help? (True for yes and anything else for no)")
INeedHelp = input("")
if INeedHelp:
print(f"{num1}÷{num2}={TrueAnswer}")
AskProblem()
else:
AskProblem()
It asked me:
How many problems do you want?
I typed 3.
And then it said in the console:
Traceback (most recent call last):
File "<string>", line 26, in <module>
File "<string>", line 15, in AskProblem
File "<string>", line 15, in AskProblem
File "<string>", line 15, in AskProblem
[Previous line repeated 994 more times]
File "<string>", line 13, in AskProblem
RecursionError: maximum recursion depth exceeded in comparison
I get no error.
First, you are formatting the if
block (from line 30 to 37) wrong, it should be in the AskPoblem
function body (with the same indent as that function's body). Also, I noticed that the if
block in the AskProblem()
(from line 15 to line 16) maybe is the problem, because when num1 > num2
, it calls the AskProblem()
function in the AskProblem()
function. This is called recursion
(Call the function in that function's body) and it may caused the error.
i think you need to define
myanswer
first.