Tuhin Tuhin - 1 year ago 153
Python Question

why there is missing keyword error in argument

compiler show the missing keyword argument for prime(list,num) but i think i do this correctly *arg for list and n for num.
But why there is show keyword argument error for print(list,num)

num = int(input('Please enter a number : '))
list = [i for i in range(2,int((num/2) +1))]

def prime(*arg , n):
for test in arg:
if n % test == 0 :
print('%d number is not a prime number.' %n)
elif n == 1 or n == 0:
print('Number is bellow 2.')
else :
print('It is a prime number')

prime(list, num)

Answer Source

Syntactically, keyword-only arguments are coded as named arguments that may appear after *arg in the arguments list. All such arguments must be passed using keyword syntax in the call.

For example, in the following, a may be passed by name or position, b collects any extra positional arguments, and c must be passed by keyword only.

>>> def kwonly(a, *b, c):
        print(a, b, c)

>>>>kwonly(1, 2, c=3)
1 (2,) 3

>>>kwonly(1, 2, 3)
TypeError: kwonly() missing 1 required keyword-only argument: 'c'

So you must pass n as as keyword argument only.

def prime(*arg , n):

n must be passed by keyword-only value

prime(list, n=num)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download