Python 为什么不';是否所有字典项都已返回?

Python 为什么不';是否所有字典项都已返回?,python,dictionary,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 Pog

这是我的密码:

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)
问题是,当我在
raw\u input
中键入
“buy”
时,我得到的输出是:

欢迎来到国际足球俱乐部
你想买球员还是卖球员?买
这是球员名单和他们的价格
(“卡卡”,“5000万美元”)
***回复关闭***
我的问题是,为什么我只为卡卡获得输出,而不是所有其他球员

for keys, values in players.items():      #Printing keys and values from dictionary
        return (keys, values) #Does this ONCE
一旦使用return,函数就终止。它将返回它循环使用的第一组键和值。因此,您将只获得一个玩家的输出。正如第二个答案所说,您可以打印它们而不是返回它们。您还可以将它们附加到列表并返回列表。此外,还需要将inpt作为参数传递给函数


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

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

 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)         

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

 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)         

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

players=['paulpogba:$8930万',
“加雷思·贝尔:8530万美元”,
“克里斯蒂亚诺·罗纳尔多:8000万美元”,
“冈萨洛·伊瓜因:7.53亿美元”,
“詹姆斯·罗德里格斯:7980万美元”,
“兹拉坦·伊布拉希莫维奇:5600万美元”,

“卡卡:5000万美元”]
尝试使用列表,而不是字典。例如:

players=['paulpogba:$8930万',
“加雷思·贝尔:8530万美元”,
“克里斯蒂亚诺·罗纳尔多:8000万美元”,
“冈萨洛·伊瓜因:7.53亿美元”,
“詹姆斯·罗德里格斯:7980万美元”,
“兹拉坦·伊布拉希莫维奇:5600万美元”,

“Kaka:$50m']
当您的
return
语句在
lst\u of_players
函数中求值时,该函数返回指定的内容,并退出该函数-因此您的
打印(键、值)
不会被执行

据我所知,您试图实现的目标可能是:

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()

当您的
return
语句在
lst\u of_players
函数中求值时,该函数返回指定的内容,并且该函数退出-因此您的
打印(键、值)
不会被执行

据我所知,您试图实现的目标可能是:

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()

您在脚本中犯了一些错误:

  • 您从未使用
    inpt
    作为函数的参数,因此需要 将其添加到函数中
  • 当返回值时,函数终止,您需要 在
    for
    循环中调用
    print
  • 最后,只需调用函数,无需 将函数指定给变量,然后像上次一样打印它 代码行可以
试着这样想:

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)

您在脚本中犯了一些错误:

  • 您从未使用
    inpt
    作为函数的参数,因此需要 将其添加到函数中
  • 当返回值时,函数终止,您需要 在
    for
    循环中调用
    print
  • 最后,只需调用函数,无需 将函数指定给变量,然后像上次一样打印它 代码行可以
试着这样想:

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)

您正在返回循环中的第一个键值,可能存储它们或直接打印它们。您需要将inpt作为参数传递到Python中
return
。完成整个函数后,您应该将值存储在列表中。您学习过Python吗?我在家学习Python,我完成了codeacademy Python课程,在youtube学习,练习一下,我每天都在编码。如果你有一些学习技巧给我,发短信给我@PEDROLOBITO如果返回循环中的第一个键值,可以直接存储或打印它们。在Python中,您需要将inpt作为参数传递
return
。完成整个函数后,您应该将值存储在列表中。您学习过Python吗?我在家学习Python,我完成了codeacademy Python课程,在youtube,practicepython学习,我每天都在编码。如果你有一些学习技巧给我,发短信给我@PedroLobitojust缺少函数参数您需要将inpt设置为参数缺少函数参数您需要将inpt设置为参数这在python中是无效的语法。也不清楚这是如何回答这个问题的。这在python中是无效的语法。也不清楚这是如何回答这个问题的。