patternpythonMinor
Mental Maths game
Viewed 0 times
mathsgamemental
Problem
I have made a mental Maths game but it seems quite long, is there anyway in which I can simplify it? And can you also help me with my other Maths game, Ten quick questions ?
```
correctq=0
actualanswer = 0
print("Welcome to Mental Maths")
print("If you chose Division rpund down to the nearest whole number")
mode = input("(A)ddition, (S)ubtraction, (M)ultiplication or (D)ivision?")
hardness = input("(E)asy, (M)edium or (H)ard?")
questions=int(input("Enter the amount of questions:"))
if mode == "A":
if hardness == "E":
from random import randint
for i in range(questions):
number1 = randint(1,100)
number2 = randint(1,100)
questionno = i + 1
answer = int(input(str(questionno) +": " + str(number1) + " + "+ str(number2) + " = "))
actualanswer = number1 + number2
if answer == actualanswer:
print("Correct!")
correctq = correctq + 1
else:
print("Wrong, the answer was ", actualanswer)
elif hardness == "M":
from random import randint
for i in range(questions):
number1 = randint(100,1000)
number2 = randint(100,1000)
questionno = i + 1
answer = int(input(str(questionno) + ": " + str(number1)+ " + " + str(number2) + " = "))
actualanswer = number1 + number2
if answer == actualanswer:
print("Correct!")
correctq = correctq + 1
else:
print("Wrong, the answer was ", actualanswer)
elif hardness == "H":
from random import randint
for i in range(questions):
number1 = randint(1000,10000)
number2 = randint(1000,10000)
questionno = i + 1
answer = int(input(str(questionno)+": "+str(number1) + " + " + str(number2) + " = "))
actualanswer = number1 + number2
if answer == actualanswer:
pr
```
correctq=0
actualanswer = 0
print("Welcome to Mental Maths")
print("If you chose Division rpund down to the nearest whole number")
mode = input("(A)ddition, (S)ubtraction, (M)ultiplication or (D)ivision?")
hardness = input("(E)asy, (M)edium or (H)ard?")
questions=int(input("Enter the amount of questions:"))
if mode == "A":
if hardness == "E":
from random import randint
for i in range(questions):
number1 = randint(1,100)
number2 = randint(1,100)
questionno = i + 1
answer = int(input(str(questionno) +": " + str(number1) + " + "+ str(number2) + " = "))
actualanswer = number1 + number2
if answer == actualanswer:
print("Correct!")
correctq = correctq + 1
else:
print("Wrong, the answer was ", actualanswer)
elif hardness == "M":
from random import randint
for i in range(questions):
number1 = randint(100,1000)
number2 = randint(100,1000)
questionno = i + 1
answer = int(input(str(questionno) + ": " + str(number1)+ " + " + str(number2) + " = "))
actualanswer = number1 + number2
if answer == actualanswer:
print("Correct!")
correctq = correctq + 1
else:
print("Wrong, the answer was ", actualanswer)
elif hardness == "H":
from random import randint
for i in range(questions):
number1 = randint(1000,10000)
number2 = randint(1000,10000)
questionno = i + 1
answer = int(input(str(questionno)+": "+str(number1) + " + " + str(number2) + " = "))
actualanswer = number1 + number2
if answer == actualanswer:
pr
Solution
You repeat this section all over the place:
You should move that into a method of its own, and just call it with the parameters:
Now you can call
Your naming is a mite hard to read, with
Last, but not least, you should put your starting code in an
from random import randint
for i in range(questions):
number1 = randint(1,100)
number2 = randint(1,100)
questionno = i + 1
answer = int(input(str(questionno) +": " + str(number1) + " + "+ str(number2) + " = "))
actualanswer = number1 + number2
if answer == actualanswer:
print("Correct!")
correctq = correctq + 1
else:
print("Wrong, the answer was ", actualanswer)You should move that into a method of its own, and just call it with the parameters:
def playAddition(min, max)
from random import randint
for i in range(questions):
number1 = randint(min, max)
number2 = randint(min, max)
questionno = i + 1
answer = int(input(str(questionno) +": " + str(number1) + " + "+ str(number2) + " = "))
actualanswer = number1 + number2
if answer == actualanswer:
print("Correct!")
correctq = correctq + 1
else:
print("Wrong, the answer was ", actualanswer)Now you can call
playGame(1, 100) and playGame(100, 1000), etc., instead of all that duplication. You will find heavy duplication between your addition(), subtraction(), etc. functions that can be removed too. I leave this as an exercise for the reader.Your naming is a mite hard to read, with
longnameswithoutanyobviouswordboundaries. You should look into the PEP8 naming convention.Last, but not least, you should put your starting code in an
if __name__ == "__main__": function. This will prevent problems when you start working with code in multiple files.Code Snippets
from random import randint
for i in range(questions):
number1 = randint(1,100)
number2 = randint(1,100)
questionno = i + 1
answer = int(input(str(questionno) +": " + str(number1) + " + "+ str(number2) + " = "))
actualanswer = number1 + number2
if answer == actualanswer:
print("Correct!")
correctq = correctq + 1
else:
print("Wrong, the answer was ", actualanswer)def playAddition(min, max)
from random import randint
for i in range(questions):
number1 = randint(min, max)
number2 = randint(min, max)
questionno = i + 1
answer = int(input(str(questionno) +": " + str(number1) + " + "+ str(number2) + " = "))
actualanswer = number1 + number2
if answer == actualanswer:
print("Correct!")
correctq = correctq + 1
else:
print("Wrong, the answer was ", actualanswer)Context
StackExchange Code Review Q#113729, answer score: 7
Revisions (0)
No revisions yet.