int()函数究竟如何工作?

时间:2017-07-05 03:58:33

标签: python

这是python 3.我发现了一些奇怪的错误: '无法分配给运营商'。请帮助解决此问题以及您可能发现的任何其他错误。感谢

Answer = 0
def Game():
    print('Hello! Enter in a number to multiply!')
    Input1 = input
    print('Ok, now enter another number to multiply!')
    Input2 = input
    print('Now let me think about that.....')
    int(Input1) * int(Input2) = Answer
    print('The answer to that is' + Answer + '!')

playAgain = 'yes'
while playAgain == 'yes':
    Game()
    print('Do you want to play again? (Yes or No?)')
    playAgain = input()

2 个答案:

答案 0 :(得分:2)

您似乎对分配如何运作感到困惑。如果要分配给变量,变量始终位于左侧。所以要设置Answer,它应该是:

Answer = ...

... = Answer

你不应该认为这只是一个声明,即两个事物在概念上是相等的,赋值是一个动作,顺序很重要:右边的表达式的值被赋给左边的变量。所以它应该是:

Answer = int(Input1) * int(Input2)

作业的左侧不能是函数调用。

答案 1 :(得分:1)

Answer = 0
def Game():
    print('Hello! Enter in a number to multiply!')
    Input1 = input()
    print('Ok, now enter another number to multiply!')
    Input2 = input()
    print('Now let me think about that.....')
    Answer = int(Input1) * int(Input2)
    print('The answer to that is' + Answer + '!')

playAgain = 'yes'
while playAgain == 'yes':
    Game()
    print('Do you want to play again? (Yes or No?)')
    playAgain = input()