显然我无法达到正确答案,我输入的所有内容都是错误的?

时间:2015-11-05 12:17:46

标签: python input multiplication

好的,所以我为自己的AS计算课程制作了自己的乘法游戏,但我遇到了很多问题,主要是围绕这一行:

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

提前致谢!

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

2 个答案:

答案 0 :(得分:1)

现在所有修复,只是做了一些调整。 Bruno和Chris解决了这个问题,错误是我的输入不是整数,在更改输入后,代码工作正常。感谢所有帮助过的人!

答案 1 :(得分:0)

不太确定,但你检查过变量类型了吗? $xmlstring = '<derivedDataType name="DerivedIntDatatypeU16"> ... </derivedDataType>'; $xml = new SimpleXMLElement($xmlstring); // Cast the SimpleXMLObject as an array $list = (array) $data->baseType; // Reset the array pointer (so we know we're at the start of the array) reset($list); // Get the key name of the first element $name = key($list); num1显然应该是数字(num2?)。也许输入是字符串?