【发布时间】:2020-12-08 22:54:09
【问题描述】:
我无法摆脱输出中的括号。我已经将 Visual Studio Code 中的默认 python 版本从 2.7 更改为 3.9。我还将其中一个打印命令更改为返回命令。任何其他建议将不胜感激。代码如下:
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)
【问题讨论】:
-
你能修正你的代码格式吗? Python 对空格敏感,所有的 def 和 for 循环看起来都在同一级别。
-
您正在返回一个数组。所以括号会保留,除非你想打印(*car)
-
请修复您的缩进,目前它会因缩进错误而失败。
-
包括一个您希望输出的示例。
-
更改返回语句 - 那些括号指定返回值是一个元组。使用
return f”{car_accessories}”或者在这种情况下更简单地使用return car_accessories
标签: python output parentheses