Python Forum
SyntaxError: positional argument follows keyword argument - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: SyntaxError: positional argument follows keyword argument (/thread-24629.html)



SyntaxError: positional argument follows keyword argument - syd_jat - Feb-23-2020

Hello All,

I am trying to do four simple things:
1. Prompt user for input,
2. Store user entered input in variable,
3. Repeat 1 and 2,
4. Add the values stored in two variables and output/display the result.

I know the following works:
print("Enter the first number:", end = ' ')
first = int(input())
print("Enter the second number:", end = ' ')
second = int(input())
print("The answer is:", first + second)
BUT I want to do it all on a single line, like below - is this even possible?
print("Enter another number:", end = ' ', third = int(input()), "And another number:", end = ' ', fourth = int(input()), "The answer is:", third + fourth)
When I try the single line, I get
Quote:SyntaxError: positional argument follows keyword argument
error message.

I am using Python3.8.1


RE: SyntaxError: positional argument follows keyword argument - jefsummers - Feb-24-2020

Getting the format, though not in 1 line:
third=int(input('Enter another number: '))
fourth=int(input('Enter yet another number: '))+third
print(f'Sum is {fourth}')



RE: SyntaxError: positional argument follows keyword argument - syd_jat - Mar-03-2020

Thanks for the post.

It isn't quite what I am after but it is an improvement none the less from fives to three lines of code.

Still would like to do it all on one line but if it can't be done then it can't be done.

What exactly is Python complaining about, is it use of variable called 'third' which isn't defined before it is referenced in the one liner print statement?


RE: SyntaxError: positional argument follows keyword argument - buran - Mar-03-2020

(Mar-03-2020, 12:57 AM)syd_jat Wrote: What exactly is Python complaining about, is it use of variable called 'third' which isn't defined before it is referenced in the one liner print statement?
it's complaining because you pass keyword arguments, like 'end' and 'third' (which at later stage will raise unexpected keyword argument error anyway) then positional arguments "And another number:"

the one-liner solution with f-strings is
print(f"The answer is: {int(input('Enter first: ')) + int(input('Enter second: '))}")

but this is really terrible un-pythonic code and I strongly advise against it