4
\$\begingroup\$

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:
                print("Correct!")
                correctq = correctq + 1
            else:
                print("Wrong, the answer was ", actualanswer)

    else:
        print("Please enter the letter E, M or H, nothing else")

elif mode == "S":
   if hardness == "E":
        from random import randint
        for i in range(questions):
            number1 = randint(50,100)
            number2 = randint(1,50)
            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(500,1000)
            number2 = randint(100,500)
            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(5000,10000)
            number2 = randint(1000,5000)
            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)

   else:
    print("Please enter the letter E, M or H, nothing else")


elif mode == "M":
   if hardness == "E":
        from random import randint
        for i in range(questions):
            number1 = randint(1,10)
            number2 = randint(1,5)
            questionno=i+1
            answer = int(input(str(questionno)+": "+str(number1)+ " x "+ 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(50,100)
            number2 = randint(1,50)
            questionno=i+1
            answer = int(input(str(questionno)+": "+str(number1)+ " x "+ 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(5,20)
            number2 = randint(1,15)
            questionno=i+1
            answer = int(input(str(questionno)+": "+str(number1) + " x " + str(number2) + " = "))
            actualanswer = number1 * number2
            if actualanswer == answer:
                print("Correct!")
                correctq = correctq + 1
            else:
                print("Wrong, the answer was "+actualanswer)




   else:
      print("Please enter the letter E, M or H, nothing else")

elif mode == "D":
   if hardness == "E":
      from random import randint
      for i in range(questions):
            number1 = randint(10,100)
            number2 = randint(1,10)
            questionno=i+1
            answer = int(input(str(questionno)+": "+str(number1)+ " ÷ "+ str(number2) + " = "))
            actualanswer = number1 / number2
            actualanswer=int(actualanswer)
            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(50,200)
            number2 = randint(10,50)
            questionno=i+1
            answer = int(input(str(questionno)+": "+str(number1)+ " ÷ "+ str(number2) + " = "))
            actualanswer = number1 / number2
            actualanswer=int(actualanswer)
            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(100,500)
            number2 = randint(50,100)
            questionno=i+1
            answer = int(input(str(questionno)+": "+str(number1)+ " ÷ "+ str(number2) + " = "))
            actualanswer = number1 / number2
            actualanswer=int(actualanswer)
            if answer == actualanswer:
                print("Correct!")
                correctq = correctq + 1
            else:
                print("Wrong, the answer was ", actualanswer)

   else:
       print("Please enter the letter E, M or H, nothing else")

else:
    print("Please enter the lettr A, S, M or D, nothing else")
print("You got " + str(correctq) + " qustions right out of " + str(questions))
\$\endgroup\$
7
\$\begingroup\$

You repeat this section all over the place:

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.

\$\endgroup\$
  • \$\begingroup\$ Thanks, thought about using a defy but didn't know how with different numbers, will try fixing it now \$\endgroup\$ – Joe Dec 12 '15 at 15:55
4
\$\begingroup\$

PEP8

Please do read the entire PEP8. From top to bottom. It's not that long and extremely useful, and should be the baseline when programming in Python. @Hosch250 pointed out about the naming, but this style guide has a lot more advices that apply to your code, to name just a few:

  • import statements should be at the beginning of the file
  • spacing around operators

Magic strings

The strings "E", "M", "H" appear multiple times. When you use the exact same literal in multiple places, put it in a variable or constant, at the top of the file, for example:

LEVEL_HIGH = "H"
LEVEL_MEDIUM = "M"
LEVEL_LOW = "L"

Augmented assignment

Instead of this:

correctq = correctq + 1

You can simplify to this shorter form:

correctq += 1

Formatting strings

Instead of string concatenation like this:

print("You got " + str(correctq) + " qustions right out of " + str(questions))

Formatted expressions are often easier to read:

print("You got {} qustions right out of {}".format(correctq, questions))

Btw there are quite a lot of typos in the texts printed by the code.

As another example, instead of this:

answer = int(input(str(questionno)+": "+str(number1) + " x " + str(number2) + " = "))

Consider this:

prompt = "{}: {} x {} = ".format(questionno, number1, number2)
answer = int(input(prompt))

How much easier is that to read?

\$\endgroup\$
  • \$\begingroup\$ Thanks you @Janos, I will take this forward in my future programs \$\endgroup\$ – Joe Dec 12 '15 at 16:15
  • \$\begingroup\$ I will read the PEP8 now \$\endgroup\$ – Joe Dec 12 '15 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.