错误:无法分配给函数调用 [英] Error: Can't Assign to Function Call

查看:401
本文介绍了错误:无法分配给函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的加法计算器,但遇到了问题。

I am trying to make a simple calculator for addition, and am experiencing a problem.

print('welcome to calculator')      # Print opening message
print(' ')  #Spacer
int(sum1) = input('number') # Get input for first number, this variable later becomes the sum : ERROR: Can't assign to function call
int(add1) = input('number') # Get input for second number : Assumed error for this line as well
sum1 = sum1 + add1      # Add the variables together to get the sum
print(str(sum1))    # Change the sum to a string and print it
loop = 1    # Set loop to True
while loop == 1:    # Continue the program until user inputs add1 as '0'
    int(add1) = input('number')     # Get value for add1
    if add1 == 0:   # Stop the program when add1 is equal to 0
        loop = 0    # Set loop to false
        break
    sum1 = sum1 + add1  # Add the variables together to get the new sum
    print(str(sum1))    # Change the sum to a string and print it
print('done!')  # Print ending message

这里的错误在第三行(我认为第四行也会有错误)。我最初只是

The error here is on the third line (And I assume the fourth would have the error too). I originally just had

sum1 = input('number')

,但是变量sum1被视为字符串,因此在第5行中,将变量sum1和add1相加时,会将字符串添加到彼此。 (例如:15 + 10 = 1510)

but the variable sum1 was being treated as a string, so in line 5 when the variables sum1 and add1 were to be added, it would add the strings to the ends of each other. (Example: 15 + 10 = 1510)

Am我在将变量转换为整数时做错了什么,或者这是克服错误的错误方法

Am I doing something wrong when I'm converting the variable to an integer, or is this the wrong way to get past the issue?

推荐答案

您要分配给 int()函数调用:

int(sum1) = input('number')

= 符号左侧的所有内容都应作为存储目标右侧产生的结果,您要让Python将内容存储在 int(sum1)中。

Everything on the left-hand side of the = sign should be a target to store the result produced by the right-hand side, and you are asking Python to store something in int(sum1).

如果要将 input()调用的结果转换为整数,则需要将其应用于返回值 input()调用,而不是存储结果的变量:

If you wanted to convert the result of the input() call to an integer, you'll need to apply it to the return value of the input() call, not the variable in which you are storing that result:

sum1 = int(input('number'))

两者均相同代码中的 add1 个案例;用途:

The same applies to both add1 cases in your code; use:

add1 = int(input('number'))

现在右侧表达式会产生一个整数结果,存储在 sum1 和<$ c中$ c> add1

Now the right-hand side expression produces an integer result, to be stored in sum1 and add1 respectively.

这篇关于错误:无法分配给函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆