【问题标题】:Output format in **Kwargs**Kwargs 中的输出格式
【发布时间】:2020-07-18 08:05:48
【问题描述】:

我想像这样格式化我的输出 - 我选择的水果是苹果鳄梨。但不确定它没有正确执行,也许我在格式化时遗漏了一些东西。你能告诉我我哪里做错了吗?

 #Kwargs
def my_func(**kwargs):
    print(kwargs)
    if 'fruit' 'veggie' in kwargs:
        print('My choice of fruits are {} {}'.format(kwargs['fruit'],['veggie']))
    else:
        print('Sorry we can not find your fruit here!')

my_func(fruit='apple', veggie= 'Avocado')

上述 sn-p 的输出如下: {'水果':'苹果','蔬菜':'鳄梨'} 抱歉,我们在这里找不到您的水果!

【问题讨论】:

  • if 语句在 kwargs 中应该是 'fruit' 而在 kwargs 中应该是 'veggie'

标签: python-3.x keyword-argument


【解决方案1】:
  1. 'fruit' 'veggie' == 'fruitveggie'
  2. ['veggie']liststr
  3. 了解 f 弦

所以,

def my_func(**kwargs):
    print(kwargs)
    if 'fruit' in kwargs and 'veggie' in kwargs:
        print(f'My choice of fruits are {kwargs["fruit"]} {kwargs["veggie"]}')
    else:
        print('Sorry we can not find your fruit here!')

my_func(fruit='apple', veggie='Avocado')

【讨论】:

  • 谢谢。甚至这一行也适用于您的代码版本。 print('我选择的水果是{} {}'.format(kwargs['fruit'],kwargs['veggie']))
猜你喜欢
  • 1970-01-01
  • 2013-11-13
  • 2019-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
相关资源
最近更新 更多