首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编写一个在字典中存储信息的函数。返回错误:"SyntaxError:‘Python3’外部函数“

编写一个在字典中存储信息的函数。返回错误:"SyntaxError:‘Python3’外部函数“
EN

Stack Overflow用户
提问于 2020-01-19 14:52:24
回答 2查看 67关注 0票数 2

第一篇文章。只是在学习编程和发布。要友善。当我从课程" Python,A crash course“中学习Python时,我收到了错误"SyntaxError:'return‘error”为了纠正这个错误或者至少在程序中更进一步,我已经更改了缩进并完全重写了代码。确切的错误是:

代码语言:javascript
复制
File "/home/billy/Desktop/python_work/solutions_exercises/Our_cars.py", line 10
    return car_dict
    ^
SyntaxError: 'return' outside function

我的代码:

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

发布于 2020-01-19 15:06:09

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

代码语言:javascript
复制
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)
票数 2
EN

Stack Overflow用户

发布于 2020-01-19 15:02:10

Python是空格敏感的。我能给你的最好的建议是坐下来重读一本好的初学者教科书的前几章。

http://shop.oreilly.com/product/0636920028154.do

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59807842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档