当前位置:网站首页>Small case of function transfer parameters

Small case of function transfer parameters

2022-07-05 13:01:00 South Lake Fishing Song

'''  Write the following function :  Function name , be called computer  It means calculating   The parameters are x,y,method, On behalf of the digital x, Numbers y, character string method  If method==add character string , return x+y  If method==sub character string , return x-y  If method==mul character string , return x*y  If method==div character string , return x/y method Set to parameters with default values , Default is default addition   Call the call parameters in the following ways   Positional arguments   Keyword parameters and parameters without default values call  '''

def compute(x,y,method = 'add'):
    if method == 'add':
        print(f'{
      x}+{
      y}',x+y)
    elif method == 'sub':
        print(f'{
      x}-{
      y}',x-y)
    elif method == 'mul':
        print(f'{
      x}*{
      y}', x * y)
    elif method == 'div':
        print(f'{
      x}/{
      y}', x / y)

compute(3,4,'add')
compute(3,4,'sub')
compute(3,4,'mul')

#  If you want to add keyword parameters , It has to be at the end 
compute(3,y=4,method='div')
# compute(3,y=4,'sub') # error: An error will be reported if the location parameter is behind the keyword argument 
compute(3,4)
compute(x=3,y=4)

 Insert picture description here

原网站

版权声明
本文为[South Lake Fishing Song]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051240052717.html