我正在学习python以提高自己的技能。我正在构建一个Web应用程序,以跟踪与我一起工作的自由职业者。我正在学习教程,但要自己制作。
代码如下:
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,当然还有每个自由职业者的输入。
这是我的第一个问题,因此,谢谢您。
答案 0 :(得分:0)
获取第一个元素后,您将立即从函数get_freelancer_titlecase(
返回。
您需要使用freelancers_in_titlecase
存储到列表append
,并在for
循环外返回末尾。
freelancers = ["juan", "andre"] #original list
def get_freelancer_titlecase(): # makes freelancers TitleCased
freelancers_in_titlecase = []
for freelancer in freelancers:
freelancers_in_titlecase.append(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) # ['Juan', 'Andre']
print(freelancers) # ['juan', 'andre']
print_freelancers_in_titlecase() # This function prints titlecased list.
答案 1 :(得分:0)
在此功能中:
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
您应该附加到“ freelancers_in_titlecase”,因此代码应更改为:
def get_freelancer_titlecase(): # makes freelancers TitleCased
freelancers_in_titlecase = []#empty list
for freelancer in freelancers:
freelancers_in_titlecase.append(freelancer.title())
return freelancers_in_titlecase
答案 2 :(得分:0)
问题出在您的get_freelancer_titlecase()
函数中。
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
此功能执行以下操作:
freelancers_in_titlecase
的空列表(而不是字典)freelancers
列表中的项目。freelancers_in_titlecase
替换为第一个自由职业者的标题大小写。试试看。
def get_freelancer_titlecase(): # makes freelancers TitleCased
freelancers_in_titlecase = [] #empty list
for freelancer in freelancers:
freelancers_in_titlecase.append(freelancer.title())
return freelancers_in_titlecase
这会将每个自由职业者的头衔加到列表中,并且仅在循环结束后才返回列表。
如果您要更改它以返回dict
,且其原始名称为键,标题大小写为值,则可以这样做:
def get_freelancer_titlecase(): # makes freelancers TitleCased
freelancers_in_titlecase = {} # empty dict
for freelancer in freelancers:
freelancers_in_titlecase[freelancer] = freelancer.title()
return freelancers_in_titlecase
答案 3 :(得分:0)
您将在第一个循环之后返回freelancers_in_titlecase
,而且您将覆盖该变量。因此,您应该将return
带到for loop
和append
变量之外,而不要替换它们:
def get_freelancer_titlecase(): # makes freelancers TitleCased
freelancers_in_titlecase = []#it is actually a list
for freelancer in freelancers:
freelancers_in_titlecase.append(freelancer.title())
return freelancers_in_titlecase
您可以考虑使用List Comprehensions重写此功能:
def get_freelancer_titlecase():
freelancers_in_titlecase = [freelancer.title() for freelancer in freelancers]
return freelancers_in_titlecase