我在学习python来提高我的技能。我正在建立一个网络应用程序来跟踪与我一起工作的自由职业者。我遵循一个教程,但使我自己。你知道吗
代码如下:
freelancers = ["juan", "andre"] #original list
def get_freelancer_titlecase(): # makes freelancers TitleCased
freelancers_in_titlecase = []#empty dict
for freelancer in freelancers:
freelancers_in_titlecase = freelancer.title()
return freelancers_in_titlecase
def print_freelancers_in_titlecase():#print freelancers in TitleCase
print_freelancers_in_titlecase = get_freelancer_titlecase()
print(print_freelancers_in_titlecase)
freelancer_list = get_freelancer_titlecase() # Place the function inside a variable
print(freelancer_list)# here is my problem, this returns only the first entry "Juan", in title case but stops there.
print(freelancers)# this is a check to see the items on the list and they are ["juan", "andre"]
为什么print(freelancer_list)
只返回一个项目?
我需要能够打电话给名单,让所有的自由职业者在标题的情况下。后来,它应该是一个字典,以容纳一个ID也,当然输入每个自由职业者。你知道吗
这是我的第一个问题,所以提前谢谢你。你知道吗
问题出在
get_freelancer_titlecase()
函数中。你知道吗此函数执行以下操作:
freelancers_in_titlecase
的空列表(不是dict)freelancers
列表中的项。你知道吗freelancers_in_titlecase
替换为第一个自由职业者的标题大小写。你知道吗试试这个。你知道吗
这将将每个自由职业者的头衔大小写添加到列表中,并且只在循环完成后返回列表。你知道吗
如果要将其更改为返回一个
dict
,原始名称作为键,标题大小写作为值,可以这样做:在此函数中:
您应该附加到“自由职业者在标题中”,因此代码应更改为:
在第一个循环之后返回
freelancers_in_titlecase
,并且重写变量。因此,应该将return
置于变量for loop
和append
之外,而不是替换它们:您可以考虑使用列表理解重写此函数:
相关问题 更多 >
编程相关推荐