第一篇文章。只是在学习编程和发布。要友善。当我从课程" Python,A crash course“中学习Python时,我收到了错误"SyntaxError:'return‘error”为了纠正这个错误或者至少在程序中更进一步,我已经更改了缩进并完全重写了代码。确切的错误是:
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)发布于 2020-01-19 15:06:09
您的代码缩进显示不正确,我只是为您写下它
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)发布于 2020-01-19 15:02:10
Python是空格敏感的。我能给你的最好的建议是坐下来重读一本好的初学者教科书的前几章。
http://shop.oreilly.com/product/0636920028154.do
def make_car(manufacturer, model, **options):
"""Make a dictionary representing our cars."""
car_dict = {
'manufacturer': manufacture.title(),
'model': model.title(),
}
# for loop is in the function
for option, value in options.items():
# this line is in the for loop
car_dict[option] = value
# this line is in the function
return car_dict
# this is your main
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)https://stackoverflow.com/questions/59807842
复制相似问题