简体   繁体   English

有人能告诉我这段代码有什么错误吗?

[英]Can someone tell me what are the mistakes in this code?

x = 4
y = 5
a = 3(x+y)

I want to know the mistakes in this code.我想知道这段代码中的错误。 I know I am a newbie but can someone help me out?我知道我是新手,但有人可以帮助我吗?

a = 3(x+y)

3 is not a function, and here you are trying to call it like one. 3 不是一个函数,在这里你试图像一个函数一样调用它。

I assume you intend multiplication.我假设你打算乘法。 Try:尝试:

a = 3 * (x + y)

The issue is the syntax .问题是语法 You are communication with the Python interpreter in the language it doesn't know ( like communicating to aliens in English ;) ).你正在用它不知道的语言与 Python 解释器进行交流就像用英语与外星人交流;) )。

When you do 3() , python thinks you are making the call to function due to the presence of () .当您执行3() ,由于()的存在,python 认为您正在调用函数。 Hence you will get error as:因此你会得到如下错误:

>>> 3()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable

In case you want to multiply (x+y) with 3 and store it in a , your syntax should be:如果您想将(x+y)乘以3并将其存储在a ,您的语法应该是:

a = 3 * (x+y)

Read a tutorial on Python's Arithmetic Operators for more details.阅读有关Python 算术运算符的教程以了解更多详细信息。

NO operator is assumed to be present .假定没有操作员存在。 It must be written explicitly.必须明确地写出来。 In the following example, the multiplication operator after b must be explicitly written.在以下示例中,必须显式编写 b 之后的乘法运算符。 It make the code workable.它使代码可行。

a = c.d.b(xy)     // usual arithmetic statement.
a = c*d*b*(x*y)   // python correct arithmetic statement.

I think this is also happened with you.我想这也发生在你身上。 try to avoid such mistakes.becuase you can not assume any operator to be present in any operation of any programming language.尽量避免这样的错误。因为你不能假设任何操作符出现在任何编程语言的任何操作中。 Keep in mind when you are trying to do any operation.当您尝试执行任何操作时请记住。

your given code:您给定的代码:

a = 3(x+y)

your correct code is :您的正确代码是:

a = 3 * (x + y)

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

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