简体   繁体   English

错误:无法分配给函数调用

[英]Error: Can't Assign to Function Call

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

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. 但是变量sum1被视为字符串,因此在第5行中,将变量sum1和add1相加时,会将字符串彼此相加。 (Example: 15 + 10 = 1510) (例如: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? 将变量转换为整数时,我做错什么了吗?还是这是解决问题的错误方法?

You are trying to assign to the result of an int() function call: 您正在尝试分配给int()函数调用的结果:

int(sum1) = input('number')

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) . =符号左侧的所有内容均应为存储右侧产生的结果的目标,并且您正在要求Python将某些内容存储在int(sum1)

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: 如果您想 input()调用的结果转换为整数,则需要将其应用于input()调用的返回值,而不是存储该结果的变量:

sum1 = int(input('number'))

The same applies to both add1 cases in your code; 同样的情况适用于代码中的两个add1情况; use: 采用:

add1 = int(input('number'))

Now the right-hand side expression produces an integer result, to be stored in sum1 and add1 respectively. 现在,右侧表达式会产生一个整数结果,分别存储在sum1add1

The statement int(sum1) = input('number') is invalid and should be sum1 = int(input('number')) in the second case you're casting the input into an int - which is what you want. 语句int(sum1) = input('number')无效,在将输入转换为int的第二种情况下,该语句应为sum1 = int(input('number')) -这就是您想要的。 In the first case you're trying to cast a function - what Python is complaining about. 在第一种情况下,您尝试转换一个函数Python在抱怨什么。

Also this should be changed everywhere in your code ! 同样,应该在代码的任何地方更改它!

您必须将其转换为整数,这是正确的方法。

x = int(input("something"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM