0

Okay so I made my own Multiplication Game for my AS Computing course, but I'm running into a number of issues, mainly this occurring around this line:

if ans == result:

    print ("That's right -- well done.\n")
    solved = solved + 1
else:
    print ("No, I'm afraid the answer is %d.\n" % result)
return solved"

The problem seems to present itself. When playing this Multiplication Game any answer that you input, seems to always be incorrect. I've posted the entire of my game below in hopes that someone can help me <3

Thanks in advance!

from random import *

solved = 0

total_num_q = 0

 def play(num1, num2, type, solved):

""" The main play function"""
def sp_type():
    type = input("Specify the question type: Multiplication: M, Addition :A, Subtraction: S, Division: D: ")
    if type not in ['M','A','S','D']:
        print("Please input only enter a valid character: ")
    return type
type = ""
while type not in ['M','A','S','D']:
    type = sp_type()
if type == "M":
    ans = input("What's %d times %d? " % (num1, num2))
    result = num1 * num2
if type == "A":
    ans = input("What's %d plus %d? " % (num1, num2))
    result = num1 + num2
if type == "S":
    ans = input("What's %d minus %d? " % (num1, num2))
    result = num1 - num2
if type == "D":
    ans = input("What's %d divided by %d? " % (num1, num2))
    result = num1/num2

if ans == result:
    print ("That's right -- well done.\n")
    solved = solved + 1
else:
    print ("No, I'm afraid the answer is %d.\n" % result)
return solved
  • 3
    Don't use `type` as a variable name. It's an important built-in metaclass and overwriting it can have several side-effects. – Klaus D. Nov 05 '15 at 12:21
  • Is this all your code? You never call play? Is this indentation formatted incorrectly or do you actually have it like that? – Luke Joshua Park Nov 05 '15 at 12:23
  • 1
    If you're using python 3.x, `input()` returns a string, not a numeric. A string will never compare equal to a numeric... – bruno desthuilliers Nov 05 '15 at 12:42
  • @Luke the rest is a part of my question 2, which I have also tested seperately and is working fine, the issue is somewhere within this section – Jamie BELCHER Nov 05 '15 at 13:43
  • @KlausD I've now replaced all type named variables to the name attempt. Now I seem to be receiving an error that attempt is not defined, but that shouldn't be too hard to fix – Jamie BELCHER Nov 05 '15 at 13:48
  • @Bruno I seeee, I've now reworded it to ans = int(input()) – Jamie BELCHER Nov 05 '15 at 13:49

2 Answers2

1

All fixed now, just took some tweaking. The issue was solved by Bruno and Chris, the error was that my inputs weren't integers, after changing the inputs, the code is working perfectly. Thanks to everyone who helped!

0

Not so sure, but have you checked the variable types? num1 and num2 should obviously be numerical (int?). Maybe input is string?

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
chris_blues
  • 129
  • 9
  • num1 and num2 aren't inputted, they're chosen at random using: num1 = randint(1, 10) num2 = randint(1, 10) – Jamie BELCHER Nov 05 '15 at 13:44
  • Sorry, you misunderstood me! It reads: num1 and num2 should obviously be numerical. So, I don't say you've inputted them. It just tries to clarify, that we're dealing with ints, so he should check what type his input is. So I don't understand your downvote, as the answer is actually correct. – chris_blues Nov 06 '15 at 14:38