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

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

问题描述

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

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 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()函数调用的结果:

You are trying to assign to the result of an int() function call:

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'))

现在右侧表达式会产生一个整数结果,分别存储在sum1add1中.

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

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

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