我在使用这段代码时遇到了问题。我似乎得到了正确的最终结果,但收到了消息“不正确,不完全是。请记住将元组解压到单独的变量中,然后使用这些变量来格式化输出。” 这是代码:
def biography_list(people):
# Iterate over each "person" in the given "people" list of tuples.
for person in people:
# Unpack the tuple into name, age, and profession variables.
name, age, profession = person[0], person[1], person[2]
# Format the required sentence and place the 3 variables
# in the correct placeholders using the .format() method.
print("{} is {} years old and works as {}.".format(name, age, profession))
# Call to the function:
biography_list([("Ira", 30, "a Chef"), ("Raj", 35, "a Lawyer"), ("Maria", 25, "an Engineer")])
# Click Run to submit code
# Output should match:
# Ira is 30 years old and works as a Chef
# Raj is 35 years old and works as a Lawyer
# Maria is 25 years old and works as an Engineer
使用以下方法,预期结果是正确的:
姓名、年龄、职业=人[0]、人[1]、人[2]
和/或
姓名、年龄、职业=人
但我还是不正确 “不完全是。记住将元组解压成单独的 变量,然后使用这些变量来格式化输出。”
请帮忙!
两种方法都可以正确解决这个问题。 这是最好的方法。我经常看到别人写这个。
name, age, profession = person
print("{} is {} years old and works as a {}".format(name,age,profession))
但是如果你这样做也是正确的:
name, age, profession = person[0],person[1],person[2]
这两个代码都可以工作,请考虑使用第一个。