Le code ci-dessous imprime les caractères individuels dans la liste au lieu d'imprimer les phrases elles-mêmes. Comment puis-je rectifier cette erreur ?

# Modify this function to return a list of strings as defined above
def list_benefits():
    things = ["More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"]
    for k in things:
        return k
# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
    return "%s is a benefit of functions!" % benefit

def name_the_benefits_of_functions():
    list_of_benefits = list_benefits()
    for benefit in list_of_benefits:
        print(build_sentence(benefit))

name_the_benefits_of_functions()

Voici ma sortie:

M is a benefit of functions!
o is a benefit of functions!
r is a benefit of functions!
e is a benefit of functions!
  is a benefit of functions!
o is a benefit of functions!
r is a benefit of functions!
g is a benefit of functions!
a is a benefit of functions!
n is a benefit of functions!
i is a benefit of functions!
z is a benefit of functions!
e is a benefit of functions!
d is a benefit of functions!
  is a benefit of functions!
c is a benefit of functions!
o is a benefit of functions!
d is a benefit of functions!
e is a benefit of functions!
>>> 
0
Isaac Attuah 8 oct. 2020 à 04:29

1 réponse

Meilleure réponse

Ici, votre fonction list_benefits() renverra toutes les phrases individuelles au lieu de la liste entière, et plus tard vous appliquerez même la liste sur les phrases individuelles. Puis modifiez list_benefits() pour renvoyer toute la liste

# Modify this function to return a list of strings as defined above
def list_benefits():
    things = ["More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"]
    return things
# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
    return "%s is a benefit of functions!" % benefit

def name_the_benefits_of_functions():
    list_of_benefits = list_benefits()
    for benefit in list_of_benefits:
        print(build_sentence(benefit))

name_the_benefits_of_functions()
1
Wasif 8 oct. 2020 à 01:34