需要帮助订购功能,以及未知错误 "Can't assign function to call"
Need help ordering functions, and unknown error "Can't assign function to call"
我有一点问题,我正在尝试创建一个程序,该程序使用函数来确定三名参赛者在登上领奖台时必须按什么顺序排列。当 运行 时,程序会像这样:
Please enter time and rider code: 53.21 HWS
Please enter time and rider code: 53.56 MAZ
Please enter time and rider code: 52.99 TMA
Please enter time and rider code:
Top 3 riders are on the podium in this order:
HWS TMA MAZ
第一名的选手将被安排在领奖台中央,第二名在左边,第三名在右边。我无法弄清楚我的代码有什么问题,但我觉得我很接近。
这是我的代码:
CYCLISTS = []
SECONDARY_LIST = {}
def placingInOrder(numeral):
placing = input("Please enter time and rider code: ")
while placing != '':
time, code = placing.split()
time = float(time)
numeral(time) = code
placing = input("Please enter time and rider code: ")
return(placing)
###MAIN ROUTINE###
placingInOrder(CYCLISTS)
firstPlace = sorted(CYCLISTS)
for i in firstPlace:
SECONDARY_LIST.append(CYCLISTS[1])
print(SECONDARY_LIST[1], SECONDARY_LIST[0], SECONDARY_LIST[2])
我不确定如何修复它,当我 运行 它时,程序显示如下:
screen capture of my error
我是一名初级程序员,刚刚结束了漫长的假期。谁能解释一下 "Can't assign function to call" 是什么意思,也许能帮我解决问题的原因?这对我有天文数字的帮助。
谢谢!
我调试你的代码,见下文
你代码numeral(time) = code,numeral是list,你把她当函数,所以错了
#!/usr/bin/env python
# coding:utf-8
'''黄哥Python'''
CYCLISTS = {}
SECONDARY_LIST = []
def placingInOrder(numeral):
placing = input("Please enter time and rider code: ")
while placing != '':
time, code = placing.split()
time = float(time)
numeral[time] = code
placing = input("Please enter time and rider code: ")
return(placing)
###MAIN ROUTINE###
placingInOrder(CYCLISTS)
firstPlace = sorted(CYCLISTS)
for i in firstPlace:
SECONDARY_LIST.append(CYCLISTS[i])
print(SECONDARY_LIST[1], SECONDARY_LIST[0], SECONDARY_LIST[2])
请输入时间和车手代码:53.21 HWS
请输入时间和骑手代码:53.56 MAZ
请输入时间和车手代码:52.99 TMA
请输入时间和车手代码:
HWS TMA MAZ
不清楚 numeral
代表什么,但您对 numeral(time) = code
的使用是您所看到的错误的原因。此语法意味着您正在尝试将 numeral
作为函数调用,并将 time
作为参数,但对于赋值语句,您需要 variable = function call
而不是 function call = variable
。无论如何,这一行都没有多大意义,因为您已经在使用用户输入的 code
作为其他东西。
如果我不得不猜测,您似乎正试图通过检查时间从 numeral
(作为字典)中查找骑车人的比赛号码。如果是这种情况,您会想要使用 racer = numeral[time]
或类似的东西。不过要小心——不能保证骑自行车的人都有独特的时间。
如果这不是您想要的,那么您需要更清楚地了解您的问题以及在您的代码中使用 numeral
的目的是什么。
我有一点问题,我正在尝试创建一个程序,该程序使用函数来确定三名参赛者在登上领奖台时必须按什么顺序排列。当 运行 时,程序会像这样:
Please enter time and rider code: 53.21 HWS
Please enter time and rider code: 53.56 MAZ
Please enter time and rider code: 52.99 TMA
Please enter time and rider code:
Top 3 riders are on the podium in this order:
HWS TMA MAZ
第一名的选手将被安排在领奖台中央,第二名在左边,第三名在右边。我无法弄清楚我的代码有什么问题,但我觉得我很接近。
这是我的代码:
CYCLISTS = []
SECONDARY_LIST = {}
def placingInOrder(numeral):
placing = input("Please enter time and rider code: ")
while placing != '':
time, code = placing.split()
time = float(time)
numeral(time) = code
placing = input("Please enter time and rider code: ")
return(placing)
###MAIN ROUTINE###
placingInOrder(CYCLISTS)
firstPlace = sorted(CYCLISTS)
for i in firstPlace:
SECONDARY_LIST.append(CYCLISTS[1])
print(SECONDARY_LIST[1], SECONDARY_LIST[0], SECONDARY_LIST[2])
我不确定如何修复它,当我 运行 它时,程序显示如下: screen capture of my error
我是一名初级程序员,刚刚结束了漫长的假期。谁能解释一下 "Can't assign function to call" 是什么意思,也许能帮我解决问题的原因?这对我有天文数字的帮助。
谢谢!
我调试你的代码,见下文
你代码numeral(time) = code,numeral是list,你把她当函数,所以错了
#!/usr/bin/env python
# coding:utf-8
'''黄哥Python'''
CYCLISTS = {}
SECONDARY_LIST = []
def placingInOrder(numeral):
placing = input("Please enter time and rider code: ")
while placing != '':
time, code = placing.split()
time = float(time)
numeral[time] = code
placing = input("Please enter time and rider code: ")
return(placing)
###MAIN ROUTINE###
placingInOrder(CYCLISTS)
firstPlace = sorted(CYCLISTS)
for i in firstPlace:
SECONDARY_LIST.append(CYCLISTS[i])
print(SECONDARY_LIST[1], SECONDARY_LIST[0], SECONDARY_LIST[2])
请输入时间和车手代码:53.21 HWS 请输入时间和骑手代码:53.56 MAZ 请输入时间和车手代码:52.99 TMA 请输入时间和车手代码: HWS TMA MAZ
不清楚 numeral
代表什么,但您对 numeral(time) = code
的使用是您所看到的错误的原因。此语法意味着您正在尝试将 numeral
作为函数调用,并将 time
作为参数,但对于赋值语句,您需要 variable = function call
而不是 function call = variable
。无论如何,这一行都没有多大意义,因为您已经在使用用户输入的 code
作为其他东西。
如果我不得不猜测,您似乎正试图通过检查时间从 numeral
(作为字典)中查找骑车人的比赛号码。如果是这种情况,您会想要使用 racer = numeral[time]
或类似的东西。不过要小心——不能保证骑自行车的人都有独特的时间。
如果这不是您想要的,那么您需要更清楚地了解您的问题以及在您的代码中使用 numeral
的目的是什么。