无法在Python中将变量传递给函数

2024-05-17 03:44:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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机器上运行此代码,但在Windows10机器上无法运行。我使用的是python3.7

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

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

Process finished with exit code 1

即使我去掉了docstring,仍然会出现以下错误:

def describe_pet(animal_type='hamster', pet_name):
                    ^
SyntaxError: non-default argument follows default argument

Tags: 代码name版本机器informationlinuxdeftype
1条回答
网友
1楼 · 发布于 2024-05-17 03:44:24

如上所述,在例外情况下,缩进是错误的。 我也看到你用5个空格作为缩进。 那会给你带来更多的麻烦。你知道吗

我建议您使用一些ide来检查您的代码是否有这样的错误,比如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')

相关问题 更多 >