Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3中打印kwargs_Python_Python 3.x_Keyword Argument - Fatal编程技术网

在Python3中打印kwargs

在Python3中打印kwargs,python,python-3.x,keyword-argument,Python,Python 3.x,Keyword Argument,我对Python比较陌生,最近遇到了kwargs。我想我了解他们以及他们是如何工作的。但是,当我尝试使用for循环打印键和值时,会出现ValueError:太多的值无法解压缩 def shop(**kwargs): sh = 1 print ("Welcome to the shop!") for i, v in kwargs: print (" ", i, ": ", v) while sh == 1: b = input

我对Python比较陌生,最近遇到了kwargs。我想我了解他们以及他们是如何工作的。但是,当我尝试使用for循环打印键和值时,会出现ValueError:太多的值无法解压缩

def shop(**kwargs):
    sh = 1
    print ("Welcome to the shop!")
    for i, v in kwargs:
        print ("    ", i, ": ", v)
    while sh == 1:
        b = input ("What would you like to buy?").lower()
        if b == i:
            Player.gold -= v
            Player.inv_plus(i)
        elif b == "exit":
            sh = 0

shop(Stone=5, Potion=10)
Player.gold是玩家拥有的金币数量,Player.inv_plusi在玩家的库存中为物品添加1。不过,这对我的问题并不重要

如果我在没有for循环的情况下打印kwargs,则效果很好。但这不是我打印时想要的格式


如果有人能解释我做错了什么,我会非常感激,因为我对它为什么不起作用感到非常困惑。

kwargs是一个dict,默认情况下,迭代它们只返回键。您需要kwargs.items


kwargs是一个dict,默认情况下,对它们进行迭代只返回键。您需要kwargs.items

只需添加一个旁注python2中的kwargs.iteritems只需添加一个旁注python2中的kwargs.iteritems
for i, v in kwargs.items():
    print ("    ", i, ": ", v)