[Tutor] Python question 4.8.2. Keyword Argument
Desiree C .Thomas
desireect at live.com
Tue Mar 5 12:52:50 EST 2024
Hello there,
I hope this reaches you well.
So I am following this https://docs.python.org/3/tutorial/controlflow.html
I'm on 4.8.2. Keyword Argument
Following the example
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
print("-- This parrot wouldn't", action, end=' ')
print("if you put", voltage, "volts through it.")
print("-- Lovely plumage, the", type)
print("-- It's", state, "!")
So this is positional arguments
parrot(1000) # 1 positional argument
parrot(voltage=1000) # 1 keyword argument
parrot(voltage=1000000, action='VOOOOOM') # 2 keyword arguments
parrot(action='VOOOOOM', voltage=1000000) # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump') # 3 positional arguments
parrot('a thousand', state='pushing up the daisies')
1. So I understand the parrot(1000) would just input into the required argument voltage
2. Parrot voltage=1000 also input into the required argument voltage
3.
parrot(voltage=1000000, action='VOOOOOM') I can enter the requirement argument and skip one of the arguments to input into action. The other non stated arguments just use the default values.
4.
parrot(action='VOOOOOM', voltage=1000000) Here I accepted that I can state on the arguments I want to filled in just as long as the requirement argument is inputted. I also accepted that the requirement argument doesn't need to be declare first either.
5.
parrot('a million', 'bereft of life', 'jump') The requirement argument was filled and the remaining two arguments were inputted in the respective order of the functions
6.
parrot('a thousand', state='pushing up the daisies') I have accepted that the requirement argument was assumed to the first argument and accept to be 'a thousand' and the declared argument was input
Please feel free of my understanding so far.
So continuing
parrot()
The requirement argument isn't inputted therefore error.
parrot(voltage=5.0, 'dead')
This is the error I get: SyntaxError: positional argument follows keyword argument on line 10
>From my understanding, I thought the voltage should accept voltage to be 5.0 and update state into 'dead'? Can I get an explaination for this error.
Thanks
Desiree Cortez-Thomas
More information about the Tutor
mailing list