Python Forum
SyntaxError: positional argument follows keyword argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SyntaxError: positional argument follows keyword argument
#1
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
Reply
#2
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}')
Reply
#3
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?
Reply
#4
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] [Errno 22] Invalid argument Junaid 0 208 Jun-12-2021, 06:02 PM
Last Post: Junaid
  TypeError: __init__() got an unexpected keyword argument 'value' Anldra12 7 1,045 May-11-2021, 06:35 PM
Last Post: deanhystad
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 859 May-02-2021, 03:45 PM
Last Post: Anldra12
  [solved] Variable number of dictionnaries as argument in def() paul18fr 11 1,287 Apr-20-2021, 11:15 AM
Last Post: paul18fr
  How to input & output parameters from command line argument shantanu97 1 549 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  int() argument Error.... but it's not :) LeoT 2 361 Feb-24-2021, 06:58 PM
Last Post: buran
  TypeError: sum() missing 1 required positional argument: 'num2' Insen 3 851 Jan-06-2021, 04:25 PM
Last Post: Insen
  How to escape OrderedDict as an argument? Mark17 2 480 Dec-23-2020, 06:47 PM
Last Post: Mark17
  Defining an object's argument whose name is stored in a variable arbiel 2 579 Dec-11-2020, 10:19 PM
Last Post: arbiel
  How to check if my argument is a tuple with 3 tuples zarox 1 441 Nov-15-2020, 06:50 PM
Last Post: DougBTX

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020