Reputation: 5
I am new to Stack overflow and have only recently began learning Python Programming. I am currently using Python 2.7 so there will be differences in my code compared to Python v3.
Anyway, I decided that I wanted to write a program that I can let my nephew and niece try out. The program created is a test with mathematics.
The code may not be the best or compact as it should be but it's my first go at it.
I have attached the code I have typed so far here. Please let me know your thoughts and any feedback.
My main question though is here:
For example, if the user decides to take on a challenge of 5 problem sets and gets 3 correct, I want the program to output, you got 3/5 correct. I have a feeling that this will be something very simple but I have not yet thought of it.
I have tried to create a variable such as answersCorrect = 0 and then just answersCorrect += 1 to increment each time an answer is correct but I can't seem to make that work so I have left it off.
Any suggestions for this?
from sys import exit
import random from random import randint import math
def addition(): problems = int(input("How many problems do you want to solve?\n"))
random.seed()
count = 0
answersRight = 0
while count < problems:
num_1 = randint(1,12)
num_2 = randint(1,12)
userAnswer = int(input("What is " + str(num_1) + "+" + str(num_2) + "? "))
generatedAnswer = (num_1 + num_2)
if userAnswer == generatedAnswer:
print "Correct"
answersRight += 1
else:
print "Sorry, the answer is ", generatedAnswer, "\n"
count += 1
print "running score of answers right ", answersRight, " out of ", problems
print "Would you like to solve more problems?"
repeatAnswer = raw_input("> ")
if repeatAnswer == "Yes" or repeatAnswer == "Y" or repeatAnswer == "YES":
start()
elif repeatAnswer == "No" or repeatAnswer == "N" or repeatAnswer == "NO":
print "Thank you for completing the test anyway..."
exit()
else:
print "You have not entered a valid response, goodbye!"
exit()
def subtraction(): problems = int(input("How many problems do you want to solve?\n"))
random.seed()
count = 0
answersRight = 0
while count < problems:
num_1 = randint(1,12)
num_2 = randint(1,12)
userAnswer = int(input("What is " + str(num_1) + "-" + str(num_2) + "? "))
generatedAnswer = (num_1 - num_2)
if userAnswer == generatedAnswer:
print "Correct"
answersRight += 1
else:
print "Sorry, the answer is ", generatedAnswer, "\n"
count += 1
print "running score of answers right ", answersRight, " out of ", problems
print "Would you like to solve more problems?"
repeatAnswer = raw_input("> ")
if repeatAnswer == "Yes" or repeatAnswer == "Y" or repeatAnswer == "YES":
start()
elif repeatAnswer == "No" or repeatAnswer == "N" or repeatAnswer == "NO":
print "Thank you for completing the test anyway..."
exit()
else:
print "You have not entered a valid response, goodbye!"
exit()
def multiply(): problems = int(input("How many problems do you want to solve?\n"))
random.seed()
count = 0
answersRight = 0
while count < problems:
num_1 = randint(1,12)
num_2 = randint(1,12)
userAnswer = int(input("What is " + str(num_1) + "x" + str(num_2) + "? "))
generatedAnswer = (num_1 * num_2)
if userAnswer == generatedAnswer:
print "Correct"
answersRight += 1
else:
print "Sorry, the answer is ", generatedAnswer, "\n"
count += 1
print "running score of answers right ", answersRight, " out of ", problems
print "Would you like to solve more problems?"
repeatAnswer = raw_input("> ")
if repeatAnswer == "Yes" or repeatAnswer == "Y" or repeatAnswer == "YES":
start()
elif repeatAnswer == "No" or repeatAnswer == "N" or repeatAnswer == "NO":
print "Thank you for completing the test anyway..."
exit()
else:
print "You have not entered a valid response, goodbye!"
exit()
def divide(): problems = int(input("How many problems do you want to solve?\n"))
random.seed()
count = 0
answersRight = 0
while count < problems:
num_1 = randint(1,12)
num_2 = randint(1,12)
userAnswer = int(input("What is " + str(num_1) + "/" + str(num_2) + "? "))
generatedAnswer = (num_1 / num_2)
if userAnswer == generatedAnswer:
print "Correct"
answersRight += 1
else:
print "Sorry, the answer is ", generatedAnswer, "\n"
count += 1
print "running score of answers right ", answersRight, " out of ", problems
print "Would you like to solve more problems?"
repeatAnswer = raw_input("> ")
if repeatAnswer == "Yes" or repeatAnswer == "Y" or repeatAnswer == "YES":
start()
elif repeatAnswer == "No" or repeatAnswer == "N" or repeatAnswer == "NO":
print "Thank you for completing the test anyway..."
exit()
else:
print "You have not entered a valid response, goodbye!"
exit()
def start(): # Welcome messages print "Welcome to this amazing game called Mathemagica!" print "I hope you are ready for an amazing challenge :D" print "My name is Mr MATHEMAGIC and i'm here to test you on your abilities" print "...." print "So, to begin please enter your Name: " name = raw_input("> ") print "Right. ", name, " Please enter your Age: " age = raw_input("> ") print "Thanks! So, ", name, " are you ready to begin your Mathematics Test? (Yes / No)" response = raw_input("> ")
# Response Dependant directing to a particular function to be CALLED
if response == "Yes" or response == "YES" or response == "Y" or response == "y":
print """Which test would you like to take first? Please select an option below:
\t1. Addition
\t2. Subtraction
\t3. Multiplication
\t4. Division
"""
# Resonse here
problemSolver = raw_input("> ")
# Evaluating what option has been selected
if problemSolver == "1":
addition()
elif problemSolver == "2":
subtraction()
elif problemSolver == "3":
multiply()
elif problemSolver == "4":
divide()
else:
print "You have not entered a valid option, Goodbye!"
exit()
elif response == "No" or response == "NO" or response == "N" or response == "n":
print "That's fine. Have a good day :)"
exit(0)
else:
print"You have not entered a valid response, Bye"
start()
Upvotes: 0
Views: 2232
Reputation: 2373
You need to be careful about where you place statements with respect to whether they are inside ifs and loops or not. Here is the correct code for addition, the others will be similar:
def addition():
problems = int(input("How many problems do you want to solve?\n"))
random.seed()
count = 0
answersRight = 0
while count < problems:
num_1 = randint(1,12)
num_2 = randint(1,12)
userAnswer = int(input("What is " + str(num_1) + "+" + str(num_2) + "? "))
generatedAnswer = (num_1 + num_2)
if userAnswer == generatedAnswer:
print "Correct"
answersRight += 1
else:
print "Sorry, the answer is ", generatedAnswer, "\n"
count += 1
print "running score of answers right ", answersRight, " out of ", problems
start()
Upvotes: 0