Firstly, you need to properly format your code for a start. Correct indents are important in python. Each indent should be exactly 4 spaces, no more, no less. It also makes your code much easier to read.
Secondly, you should declare your methods and classes first, then your code to call those methods.
Thirdly, you need to be careful of initialising variables that then get called in the function (ie correct = 0 and incorrect = 0). This is generally bad practice. In order to work they need to be re-declared in method as global. In your case, just declare them in method ONLY. Your code should look more like:
def math_questions():
correct = 0
incorrect = 0
ans1 = input("Question 1: Ron had 6 bags of flour but used up 4 bags on Sunday. How many bags of flour did he have left?: ")
if ans1 == "2":
print("\nCorrect! " + ans1 + " is the right answer.")
correct += 1
else:
print("\nYou are way off. Start from the very beginning, or continue with the next one.")
incorrect += 1
print("")
print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))
print("")
ans2 = input("Question 2: The Miller parking lot was full at the capacity of 15 this morning, now at the end of the day, 12 left the lot. How many cars are left in the parking lot?: ")
if ans2 == "3":
print("\nCorrect! " + ans2 + " is the right answer.")
correct += 1
else:
print("\nYou are way off. Start from the very beginning, or continue with the next one.")
print("")
print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))
ans3 = input("Question 3: Mrs. Sheridan has 11 cats. How many more cats does Mrs. Sheridan need to have 43 cats?: ")
if ans3 == "32":
print("\nCorrect! " + ans3 + " is the right answer.")
correct += 1
else:
print("\nYou are way off. Start from the very beginning, or continue with the next one.")
print("")
print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))
ans4 = input("Shelly bought a bed sheet for $1699. She gave the shopkeeper $2000. What amount will be returned by the shopkeeper?: ")
if ans4 == "301":
print("\nCorrect! " + ans4 + " is the right answer.")
correct += 1
else:
print("\nYou are way off. Start from the very beginning, or continue with the next one.")
print("")
print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))
name = input("Please enter your name: ")
print("")
run = int(input("Hello " + name + ", would you like to play a game? (Enter 1 to continue, 2 to exit): "))
print("")
while run == 1:
print("I will be asking a series of questions in each categories: mathematical, scientifical. Each question getting harder.")
math_questions()
Furthermore, I would recommend breaking down your code into smaller chunks and re-use code where possible. All your if statements have the same format and lend themselves to being put into another function or some loop. Look at:
def question_answered_correctly(new_question, correct_answer):
ans = input(new_question)
return ans == correct_answer:
def math_questions():
correct = 0
incorrect = 0
question_list = [
{
'question': "Question 1: Ron had 6 bags of flour but used up 4 bags on Sunday. How many bags of flour did he have left?: ",
'answer': '2'},
{
'question': "Question 2: The Miller parking lot was full at the capacity of 15 this morning, now at the end of the day, 12 left the lot. How many cars are left in the parking lot?: ",
'answer': '3'},
{
'question': "Question 3: Mrs. Sheridan has 11 cats. How many more cats does Mrs. Sheridan need to have 43 cats?: ",
'answer': '32'},
{
'question': "Question 4: Shelly bought a bed sheet for $1699. She gave the shopkeeper $2000. What amount will be returned by the shopkeeper?: ",
'answer': '301'}
]
for question in question_list:
if question_answered_correctly(question['question'], question['answer']):
print("\nCorrect! " + question['answer'] + " is the right answer.")
correct += 1
else:
print("\nYou are way off. Start from the very beginning, or continue with the next one.")
incorrect += 1
print("")
print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))
name = input("Please enter your name: ")
print("")
run = int(input("Hello " + name + ", would you like to play a game? (Enter 1 to continue, 2 to exit): "))
print("")
while run == 1:
print("I will be asking a series of questions in each categories: mathematical, scientifical. Each question getting harder.")
math_questions()