gpt4 book ai didi

python - 编写一个将信息存储在字典中的函数。 Python3 错误 : "SyntaxError: ' return' outside function"

转载 作者:行者123 更新时间:2023-12-04 10:43:52 26 4
gpt4 key购买 nike

第一个帖子。刚刚学习编程和发布。善待。
在从“Python,速成类(class)”类(class)学习 Python 时,我收到错误“SyntaxError: 'return' external function”
为了纠正错误或至少在程序中更进一步,我更改了缩进并完全重写了代码。
确切的错误是:

File "/home/billy/Desktop/python_work/solutions_exercises/Our_cars.py", line 10
return car_dict
^
SyntaxError: 'return' outside function

我的代码:
def make_car(manufacturer, model, **options):
"""Make a dictionary representing our cars."""
car_dict = {
'manufacturer': manufacture.title(),
'model': model.title(),
}
for option, value in options.items():
car_dict[option] = value
return car_dict
our_forrester = make_car('subaru', 'forrester', color='black', all_wheel_drive=True, AC=True)
print(our_forrester)
our_tacoma = make_car('toyota', 'tacoma', color='dark green', four_wheel_drive=True, AC=True)
print(our_tacoma)

最佳答案

您的代码缩进没有正确显示,我只是为您写下

def make_car(manufacturer, model, **options):
"""Make a dictionary representing our cars."""
car_dict = {
'manufacturer': manufacture.title(),
'model': model.title(),
}

for option, value in options.items():
car_dict[option] = value

return car_dict

our_forrester = make_car('subaru', 'forrester', color='black',
all_wheel_drive=True, AC=True)

print(our_forrester)

our_tacoma = make_car('toyota', 'tacoma', color='dark green',
four_wheel_drive=True, AC=True)

print(our_tacoma)

关于python - 编写一个将信息存储在字典中的函数。 Python3 错误 : "SyntaxError: ' return' outside function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59807842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com