简体   繁体   English

为什么会出现此语法错误:关键字不能为表达式

[英]Why am I getting this syntax error: keyword can't be an expression

def multiply():
    number_1 = int (input ("Please insert the first number for multiplication:"))
    number_2 = int (input ("Please insert the second number for multiplication:"))
    result = str (number_1*number_2)
    return print (number_1*number_2=result)
multiply()

I am getting the Following error when trying to execute the following function. 尝试执行以下功能时出现以下错误。

    return print (number_1*number_2=result)               
                 ^
SyntaxError: keyword can't be an expression

You are defining a keyword argument by using = : 您正在使用=定义关键字参数:

...(number_1*number_2=result)
                     ^

That makes everything before the = a Python expression, namely number_1 * number_2 . 这使得= 之前的所有内容都是Python表达式,即number_1 * number_2 Python doesn't allow this. Python不允许这样做。

If you wanted to print out a nicely formatted expression, you'll have to use separate string arguments: 如果要打印出格式正确的表达式,则必须使用单独的字符串参数:

print(number_1, '*', number_2, '=', result)

Python writes out separate arguments with a space to separate them (but you can use sep='...' to set a different string to separate them, including the empty string to have no separation at all). Python用空格分隔写出单独的参数(但是您可以使用sep='...'设置不同的字符串来分隔它们,包括字符串根本没有分隔符)。

Note that you don't actually have to convert result to a string here, the print() function converts all arguments to strings before writing them to the console. 请注意,您实际上不必在此处将result转换为字符串,在将参数写入控制台之前, print()函数会将所有参数转换为字符串。

You could also learn about Python string formatting , which gives you a bit more control over the whitespace handling, as well as alignment end value formatting, for example. 您还可以了解Python字符串格式化 ,例如,它可以使您对空白处理以及对齐最终值格式有更多的控制。

The following would print out your numbers and the result as part of a format string with the same amount of spaces: 以下代码将您的数字和结果打印为带有相同空格的格式字符串的一部分:

print('{} * {} = {}'.format(number_1, number_2, result))

Each {} placeholder is then filled with the next argument you passed into the str.format() method . 然后,每个{}占位符都填充有您传递给str.format()方法的下一个参数。 You can remove the spaces here too if you wanted to. 如果需要,您也可以在此处删除空格。

Last, but not least, the print() function always returns None . 最后但并非最不重要的一点, print()函数始终返回None You don't have to return that from your function, remove the return . 您不必从函数中返回该值,请删除return Your actual use of the multiply function doesn't use the returned value anyway: 您实际使用multiply函数不会使用返回值:

def multiply():
    number_1 = int(input("Please insert the first number for multiplication:"))
    number_2 = int(input("Please insert the second number for multiplication:"))
    result = number_1 * number_2
    print(number_1, '*', number_2, '=', result)

multiply()

Why am I getting this syntax error: keyword can't be an expression 为什么会出现此语法错误:关键字不能为表达式

Because what you write in parentheses after a function name (including print in python3) are arguments of the function. 因为您在函数名称(包括python3中的print )后面的括号中编写的内容是函数的参数。 Any argument written in the form keyword=value is a keyword argument . keyword=value形式编写的任何参数都是关键字arguments You cannot use an expression (here a product) as a keyword. 您不能将表达式(此处为产品)用作关键字。

I assume you want output like 我假设你想要输出像

number_1*number2 = 6

The first part of this is some fixed text you want printed. 第一部分是要打印的一些固定文本。 Thus you need to put in in a string and print that. 因此,您需要输入一个字符串并打印出来。 You can print multiple things like print(a, b) . 您可以打印多种内容,例如print(a, b) This should be enough for you to figure out 这应该足以让您弄清楚

You have two issues: 您有两个问题:

You are writing an expression inside a print statement. 您正在打印语句中编写表达式。 Instead you want to make a formatted string and substitute the values into the string: 相反,您想创建一个格式化的字符串并将值替换为字符串:

print("%d*%d=%s" % (number_1, number_2, result))

Secondly, you don't need to return it. 其次,您不需要返回它。

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

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