dimanche 15 mars 2015

Math equation generator program

I've been creating this math equation generator program that will ask you question depending on what difficulty you put. I've got it running at the moment but when the programs ask what difficulty you want it keeps on repeating it and I don't know how to solve it. I'd just like some tips on what I could do to solve it.



import random
import time
import sys


correct = 0 #Amount the user has gotten correct
ans = 0 #Holds the answer to the question
lastName = str() #holds last name
firstName = str() #holds first name
className = str() #holds user's form
difficulty = int() #where user input what diffliculty they want
Beginner = 0
beginner = 0
Intermediate = 0
intermediate = 0
Advanced = 0
advanced = 0

F = firstName = raw_input("Please enter your first name: ").title()
L = lastName = raw_input("Please enter your surname: ").title()
C = className = raw_input("Please enter your form class: ").title()
print "Hi", F, L,
print ''

time.sleep(1)

while True:
difficulty = int(input("Please select a difficulty level: Beginner, Intermediate, or Advanced: "))

if difficulty == "Beginner":
def multiplication(): #Creates a multiplication question in beginner
global ans
numOne, numTwo = random.randint(0,10), random.randint(0,10)
print "What is", numOne, "*" , numTwo, "?"
ans = (numOne*numTwo)

def addition(): #Creates a addition question in beginner
global ans
numOne, numTwo = random.randint(0,10), random.randint(0,10)
print "What is", numOne, "+" , numTwo, "?"
ans = (numOne+numTwo)

def subtraction(): #Creates a subtraction question in beginner
global ans
numOne, numTwo = random.randint(0,10), random.randint(0,10)
print "What is", numOne, "-" , numTwo, "?"
ans = (numOne-numTwo)

operation = [multiplication,subtraction,addition] #holds all of the opperators
randOperation = random.choice(operation) #chooses a random operator

if difficulty == "Intermediate":
def multiplication(): #Creates a multiplication question in intermediate
global ans
numOne, numTwo = random.randint(10,20), random.randint(10,20)
print "What is", numOne, "*" , numTwo, "?"
ans = (numOne*numTwo)

def addition(): #Creates a addition question in intermediate
global ans
numOne, numTwo = random.randint(10,20), random.randint(10,20)
print "What is", numOne, "+" , numTwo, "?"
ans = (numOne+numTwo)

def subtraction():#Creates a subtraction question in intermediate
global ans
numOne, numTwo = random.randint(10,20), random.randint(10,20)
print "What is", numOne, "-" , numTwo, "?"
ans = (numOne-numTwo)

operation = [multiplication,subtraction,addition] #holds all of the opperators
randOperation = random.choice(operation) #chooses a random operator

if difficulty == "Advanced":
def multiplication(): #Creates a multiplication question in advanced
global ans
numOne, numTwo = random.randint(20,35), random.randint(20,35)
print "What is", numOne, "*" , numTwo, "?"
ans = (numOne*numTwo)

def addition(): #Creates a addition in advanced
global ans
numOne, numTwo = random.randint(20,35), random.randint(20,35)
print "What is", numOne, "+" , numTwo, "?"
ans = (numOne+numTwo)

def subtraction(): #Creates a subtraction question in advanced
global ans
numOne, numTwo = random.randint(20,35), random.randint(20,35)
print "What is", numOne, "-" , numTwo, "?"
ans = (numOne-numTwo)

operation = [multiplication,subtraction,addition] #holds all of the opperators
randOperation = random.choice(operation) #chooses a random operator


def main(): #main game loop - ask questions and checks it against answer, stops are a give amount of questions
question = 0
user_score = 0
randOperation = random.choice(operation)

while True:
try:
randOperation()
randOperation = random.choice(operation)
if question >= 12:
break
userInput = int(input("Enter the answer: "))
if userInput == ans:
print("Correct!" + "\n")
user_score += 1
question += 1
else:
print("Incorrect!" + "\n")
question += 1
except ValueError:
print("I'm sorry that's invalid")
question += 1

main() #initializes the function

print(firstName, lastName , "you scored" , user_score , "out of 10") #shows the user's score and name

user_name = firstName + ' ' + lastName
function(user_score,user_name)


def endMenu():
while True:
try:
options = int(input('''Press '1' to view users' scores,
press '2' to restart the test,
press '3' to exit the game,

Enter option here: '''))
except ValueError:
print("I'm sorry that was invalid...")

if options == 3: #exits the game...
sys.exit()

elif options == 2: #starts the game loop again because it's in a function
main()

elif options == 1: #displays everything on the .txt file
f = open('userScore.txt', 'r')
print(f.read())
print()
endMenu()

else:
print("Sorry, I don't understand. Please try again...")
print()
endMenu()

endMenu()




Aucun commentaire:

Enregistrer un commentaire