Why am I getting this syntax error: 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 doesn’t allow this.

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

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.

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.

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. You can remove the spaces here too if you wanted to.

Last, but not least, the print() function always returns None. You don’t have to return that from your function, remove the return. Your actual use of the multiply function doesn’t use the returned value anyway:

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

Leave a Comment