为什么不返回所有字典项?

时间:2017-07-11 17:35:56

标签: python dictionary

这是我的代码:

print ("Welcome to International Football Transfers")
inpt = raw_input("Do you want to buy player or sell?")

def lst_of_players():
    if inpt == "buy":
        print ("Here is list of players and their prices")
    players = {
    'Paul Pogba' : '$89,3m',
    'Gareth Bale' : '$85,3m',
    'Cristiano Ronaldo' : '$80m',
    'Gonzalo Higuain' : '$75,3m',
    'James Rodriguez' : '$79,8m',
    'Zlatan Ibrahimovic' : '$56m',
    'Kaka' : '$50m'
    }
    for keys, values in players.items():      #Printing keys and values from dictionary
        return (keys, values)
    print (keys,values)

plyrs = lst_of_players()
print (plyrs)

问题是当我在"buy"中输入raw_input时,我得到的输出是:

Welcome to International Football Transfers
Do you want to buy player or sell?buy
Here is list of players and their prices
('Kaka', '$50m')

***Repl Closed***

我的问题是为什么我只为Kaka而不是所有其他玩家获得输出?

5 个答案:

答案 0 :(得分:1)

将输入传递给文件并打印结果而不是返回结果。

 def lst_of_players(inpt):       
    if inpt == "buy":      
        print ("Here is list of players and their prices")
        players = {
       'Paul Pogba' : '$89,3m',
       'Gareth Bale' : '$85,3m',
       'Cristiano Ronaldo' : '$80m',
       'Gonzalo Higuain' : '$75,3m',
       'Zlatan Ibrahimovic' : '$56m',
       'Kaka' : '$50m'
        }
       for keys, values in players.items():  

          print (keys,values)



print ("Welcome to International Football Transfers")
inpt = raw_input("Do you want to buy player or sell?")
lst_of_players(inpt)         

答案 1 :(得分:1)

当在return函数内部评估lst_of_players语句时,该函数返回指定的内容,并退出该函数 - 因此您的print (keys,values)未被执行。< / p>

据我所知,你想要实现的目标可以实现为:

print ("Welcome to International Football Transfers")
inpt = raw_input("Do you want to buy player or sell?")

def lst_of_players():
    if inpt == "buy":
        print ("Here is list of players and their prices")
    players = {
    'Paul Pogba' : '$89,3m',
    'Gareth Bale' : '$85,3m',
    'Cristiano Ronaldo' : '$80m',
    'Gonzalo Higuain' : '$75,3m',
    'James Rodriguez' : '$79,8m',
    'Zlatan Ibrahimovic' : '$56m',
    'Kaka' : '$50m'
    }
    for key, value in players.items():
        print("{}: {}".format(key, value))

lst_of_players()

答案 2 :(得分:1)

你在剧本中犯了一些错误:

  • 你从不使用inpt作为你职能的参数,所以你需要 将其添加到您的功能中。
  • 返回值时,函数终止,您需要 在print循环中调用for
  • 最后,你只需要打电话给你的功能,没有必要 将您的函数分配给变量,然后像上次一样打印它 代码行。

尝试这样的想法:

def lst_of_players(inpt):
    if inpt == "buy":
        print ("Here is list of players and their prices")
    players = {
    'Paul Pogba' : '$89,3m',
    'Gareth Bale' : '$85,3m',
    'Cristiano Ronaldo' : '$80m',
    'Gonzalo Higuain' : '$75,3m',
    'James Rodriguez' : '$79,8m',
    'Zlatan Ibrahimovic' : '$56m',
    'Kaka' : '$50m'
    }
    for key, value in players.items():
        print(key, value)

lst_of_players(inpt)

答案 3 :(得分:0)

for keys, values in players.items():      #Printing keys and values from dictionary
        return (keys, values) #Does this ONCE

使用return后,函数终止。它将返回第一组键和它循环的值。因此,您只能获得一名玩家的输出。正如第二个答案所说,你可以打印它们而不是返回它们。您还可以将它们附加到列表并返回列表。此外,您需要将inpt作为参数传递给您的函数。

答案 4 :(得分:-1)

尝试使用列表,而不是字典。例如:

players = ['Paul Pogba : $89,3m',
    'Gareth Bale : $85,3m',
    'Cristiano Ronaldo : $80m',
    'Gonzalo Higuain : $75,3m',
    'James Rodriguez : $79,8m',
    'Zlatan Ibrahimovic : $56m',
    'Kaka : $50m']