输出中的括号
Parentheses in output
我无法去掉输出中的括号。我已经将 Visual Studio 代码中 python 的默认版本从 2.7 更改为 3.9。我还将其中一个打印命令更改为 return 命令。任何其他建议将不胜感激。这是代码:
def build_car(year, color, make, model, *car_accessories):
print(f"This car is a {year} {color} {make} {model} with the following accessories--:")
for accessory in car_accessories:
return (f"{car_accessories}")
car = build_car('2021', 'tan' ,'ford','focus',
'leather seats',
'tinted windows',
'all wheel drive')
print(car)
问题是 *car_accessories
是一个元组,当您打印该元组时,它会使用周围的括号进行格式化。您总是可以使用类似下面的方法将元组转换为字符串。获得字符串后,您可以使用 f 字符串应用任何格式。
return ', '.join(car_accessories)
build_car
拿不定主意。它打印一些东西,返回其他东西供调用者打印。非常混乱。如果该函数的作用只是格式化字符串,则该函数的用途更广泛。让调用者决定接下来要做什么。由于 car_accessories
是项目的元组,您可以用逗号“连接”它们。整个格式化步骤可以用一个 f 字符串完成。
def build_car(year, color, make, model, *car_accessories):
return f"""This car is a {year} {color} {make} {model} with the following accessories:
{", ".join(car_accessories)}
"""
car = build_car('2021', 'tan' ,'ford','focus',
'leather seats',
'tinted windows',
'all wheel drive')
print(car)
您应该在功能和 return 完成的产品中构建完整的描述。在函数和调用程序之间拆分打印很尴尬。
def build_car(year, color, make, model, *car_accessories):
description = f"This car is a {year} {color} {make} {model} with the following accessories: "
description += ' '.join(car_accessories)
return description
car = build_car('2021', 'tan' ,'ford','focus',
'leather seats',
'tinted windows',
'all wheel drive')
print(car)
输出:
This car is a 2021 tan ford focus with the following accessories: leather seats tinted windows all wheel drive
您的原始代码也有一个问题,它 return
一碰到第一个附件就会立即消失。请查看 return
的工作原理,以免再次出现此错误。
我无法去掉输出中的括号。我已经将 Visual Studio 代码中 python 的默认版本从 2.7 更改为 3.9。我还将其中一个打印命令更改为 return 命令。任何其他建议将不胜感激。这是代码:
def build_car(year, color, make, model, *car_accessories):
print(f"This car is a {year} {color} {make} {model} with the following accessories--:")
for accessory in car_accessories:
return (f"{car_accessories}")
car = build_car('2021', 'tan' ,'ford','focus',
'leather seats',
'tinted windows',
'all wheel drive')
print(car)
问题是 *car_accessories
是一个元组,当您打印该元组时,它会使用周围的括号进行格式化。您总是可以使用类似下面的方法将元组转换为字符串。获得字符串后,您可以使用 f 字符串应用任何格式。
return ', '.join(car_accessories)
build_car
拿不定主意。它打印一些东西,返回其他东西供调用者打印。非常混乱。如果该函数的作用只是格式化字符串,则该函数的用途更广泛。让调用者决定接下来要做什么。由于 car_accessories
是项目的元组,您可以用逗号“连接”它们。整个格式化步骤可以用一个 f 字符串完成。
def build_car(year, color, make, model, *car_accessories):
return f"""This car is a {year} {color} {make} {model} with the following accessories:
{", ".join(car_accessories)}
"""
car = build_car('2021', 'tan' ,'ford','focus',
'leather seats',
'tinted windows',
'all wheel drive')
print(car)
您应该在功能和 return 完成的产品中构建完整的描述。在函数和调用程序之间拆分打印很尴尬。
def build_car(year, color, make, model, *car_accessories):
description = f"This car is a {year} {color} {make} {model} with the following accessories: "
description += ' '.join(car_accessories)
return description
car = build_car('2021', 'tan' ,'ford','focus',
'leather seats',
'tinted windows',
'all wheel drive')
print(car)
输出:
This car is a 2021 tan ford focus with the following accessories: leather seats tinted windows all wheel drive
您的原始代码也有一个问题,它 return
一碰到第一个附件就会立即消失。请查看 return
的工作原理,以免再次出现此错误。