[英]Python can't assign operator error
I am new to python and started coding a little less than a week ago. 我是python的新手,不到一周前就开始编写代码。 I decided that I want to try and build a single diget calculator.
我决定要尝试构建一个单一的diget计算器。 I know my code may not be very well written and suggestions appreciated but please dont just hate on it.
我知道我的代码可能写得不太好,建议也值得赞赏,但请不要只是讨厌它。 Well now to the code.
现在到代码了。 I am getting the error on the code saying that it cant assign an opperator.
我在代码上收到错误消息,说它无法分配操作员。 I know that it means it can not make the variable into from string to int.
我知道这意味着它不能使变量从字符串到整数。 But I am not sure how to fix it.
但是我不确定如何解决它。 Also I get the error on line 25, 28, 31, 34. Thanks!
我在第25、28、31、34行也得到了错误。谢谢!
calculate = input("Enter the problem in the format x + y = z ")
opperations = ["+", "-", "*", "/"]
numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
space = " "
def opperation():
if opperations == "+":
A = 1
elif opperations == "-":
A = 2
elif opperations == "*":
A = 3
elif opperations == "/":
A = 4
if calculate[0] in numbers:
if len(calculate) > 1:
if calculate[1] == space:
calculate[0] = a
a = int(a)
calculate[4] = b
b = int(b)
if A == 1:
a + b = c
print (c)
elif A == 2:
a - b = c
print (c)
elif A == 3:
a * b = c
print(c)
elif A == 4:
a / b = c
print(c)
else:
print("Sorry invalid text")
else:
print("Sorry invalid text")
else:
print("Sorry invalid text")
else:
print("Sorry invalid text")
You cannot have an operation in the left-hand side of an assignment. 您不能在作业的左侧进行操作。 You should do the operation in the right-hand side :
您应该在右侧进行操作:
c = a + b
c = a - b
Also, you are using b
in the line 另外,您在行中使用
b
calculate[4] = b
b = int(b)
before declaring it. 在声明之前 。 You have to declare it first.
您必须先声明它。 So, you may want to check your logic there.
因此,您可能要在那里检查您的逻辑。
Your problem is that you use =
operator in wrong way. 您的问题是您以错误的方式使用
=
运算符。 How it works is that it evaluates what is on the right , and assigns it to what is on the left . 它的工作方式是评估右侧的内容 ,并将其分配给左侧的内容 。 So instead
a + b = c
, it should be: 所以
a + b = c
,应该是:
c = a + b
What you have tried to do, was to assign value of c
to the expression a + b
, which does not make any sense. 您试图做的是将
c
值赋给表达式a + b
,这没有任何意义。
Your issues are these lines: 您的问题是这些行:
a + b = c
a - b = c
a * b = c
a / b = c
Python doesn't work that way. Python不能那样工作。 You're trying to do
(a + b) = c
, which is the same as trying to do, say, 5 = c
. 您正在尝试执行
(a + b) = c
,这与尝试执行5 = c
。
If you want c
to be the result of the operation on a
and b
, you need to flip those lines around, ie c = a + b
. 如果希望
c
成为对a
和b
进行运算的结果,则需要翻转这些线,即c = a + b
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.