mardi 7 avril 2015

repetition of the variable in the function

I am writing a program which works as a mathematics tester. This program partially works fine when the user choose 1 as a difficulty and makes 3 mistakes it exit the loop and so on. But due to repetition of the variables, if statement in the loop and in the function making it not work properly. Like if the user doesn't make any mistake the function loop doesn't end it keeps going. Need some guidance on this. Thanks



import random
print ("Welcome to Maths Tester Pro. \nSelect a difficulty:\n1) Easy\n2) Medium\n3) hard")
while True:
difficulty = input("> ") #input variable
#repetition of the entire if statement
if difficulty == '1': #this statement works only if the user difficulty = 1
lives = '3' #repetition
maxNum = '10' #repetition
print("\nIf “Easy” was chosen… lives = 3 and maxNum = 10\n")
break
elif difficulty == '2': #this statement works only if the user difficulty = 2
print("\nIf “Easy” was chosen… lives = 2 and maxNum = 25\n")
lives = '2'#repetition
maxNum = '25'#repetition
break
elif difficulty == '3': #this statement works only if the user difficulty = 3
print("\nIf “Easy” was chosen… lives = 1 and maxNum = 50\n")
lives = '1'#repetition
maxNum = '50'#repetition
break


def Generatequestions(maxNum=50, lives=3): #repetition of maxNum and lives
finish = False
questioncounter = 1
correctquestions = 0 #repetition
operator = random.choice("+-")
while not finish:
for QuestionCounter in range(10):
#repetition again of the if statements
if difficulty == '1':
number1 = random.randint(1,maxNum-40)
number2 = random.randint(1,maxNum-40)
pass
elif difficulty == '2':
number1 = random.randint(1,maxNum-25)
number2 = random.randint(1,maxNum-25)
pass
elif difficulty == '3':
number1 = random.randint(1,maxNum)
number2 = random.randint(1,maxNum)
pass
print("Question", questioncounter, "of 10,", lives, "lives remaining.\n")
questioncounter += 1
userinput = input("What is " + (str(number2) + str(operator) + str(number1) + "?"))
answer = eval(str(number2)+ str(operator)+str(number1))
if str(userinput) == str(answer):
print("Correct.\n")
correctquestions += 1
else:
lives -= 1
print("incorect ",answer, "is the answer.\n")
if lives == 0:
print("Out of lives, game over!")
return False
break
else:
finish=True
Generatequestions()

correctquestions = 0 #repetition causing it not to work properly

print ("\nResults\nYou scored ",correctquestions,"/10." )
percentage = int((correctquestions/question)*100)
if correctquestions >= question/2:
print (percentage,"% - you passed")
else:
print (percentage,"% - you failed")

Aucun commentaire:

Enregistrer un commentaire