小编tit*_*ako的帖子

可选参数 Python

必须编写一些代码来定义一个可以使用两个或三个参数的函数。

该函数应采用三个参数:

  • 温度,浮动
  • 天气,字符串
  • is_celsius,一个布尔值

如果温度低于冰点(如果 is_celsius 为 False,则为 32,如果 is_celsius 为 True,则该函数应返回 True)或天气“下雪”。否则,它应该返回 False。

但是请注意, is_celsius 应该是一个可选参数。如果函数调用没有为 is_celsius 提供值,则假定它为 True。

我写了这个

def snowed_in(temperature, weather, **cels):
    if weather =="snowy":
        return True
    elif weather=="sunny":
        if 'is_celsius = False' in cels:
            if temperature<32:
                return True
    elif 'is_celsius = True' in cels:
        if temperature<0:
            print ('ad')    
            return True
    elif 'is_celsius = True' not in cels:
        if temperature<0:
            return True
    else:
        return False
Run Code Online (Sandbox Code Playgroud)

以及以下电话

print(snowed_in(15, "sunny")) #Should print False
print(snowed_in(15, "sunny",is_celsius = False)) #Should …
Run Code Online (Sandbox Code Playgroud)

python debugging arguments optional

4
推荐指数
1
解决办法
6100
查看次数

标签 统计

arguments ×1

debugging ×1

optional ×1

python ×1