无法将变量传递给Python函数

问题描述 投票:-4回答:1
def describe_pet(animal_type='hamster', pet_name):
 """displays information about a pet"""
     print("\nI have a " + animal_type+ ".")
     print("My " +animal_type +"'s name is "+pet_name.title()+".")
describe_pet(animal_type='hamster', pet_name='harry')

我无法在Python中运行以下代码。我能够在Linux机器上运行此代码,但在Windows 10机器上却无法运行。我正在使用python 3.7

当我运行代码时,出现以下错误消息。请告知操作系统或python版本是否有问题,因为我不确定linux机器上安装了什么版本的python!

 """Display information about a pet."""
                                         ^
IndentationError: expected an indented block

Process finished with exit code 1

即使我摆脱了文档字符串,我仍然收到以下错误:

def describe_pet(animal_type='hamster', pet_name):
                    ^
SyntaxError: non-default argument follows default argument
python function
1个回答
1
投票

如上所述,但缩进是错误的。我还看到您使用5个空格作为缩进。将来可能会给您带来更多麻烦。

我建议您使用一些想法来检查您的代码中是否存在诸如pydev之类的错误,例如pycharmeclipse。作为奖励,您将获得如何写good styled code的建议。

def describe_pet(animal_type='hamster', pet_name):
    """displays information about a pet"""
    print("\nI have a " + animal_type+ ".")
    print("My " +animal_type +"'s name is "+pet_name.title()+".")

describe_pet(animal_type='hamster', pet_name='harry')
© www.soinside.com 2019 - 2024. All rights reserved.