为什么我的程序没有在列表中的第一项?

2024-06-02 05:57:22 发布

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

我正在学习Python第3卷的速成课程,我正在学习第8-12章,这是关于函数的。以下是我目前的代码:

def sandwich_builder(bread,*items):    
    print(f"What type of bread do you want?")
    bread=input("Type of Bread:")
    print(f"Making your sandwich on {bread} bread with the following items:")
    for item in items:
            print(f"-{item}")

sandwich_builder('turkey','bacon','avocado','cheddar','mayonnaise','tomato','lettuce')

但是,我的输出如下所示:

Making your sandwich on sourdough bread with the following items:
-bacon

-avocado

-cheddar

-mayonnaise

-tomato

-lettuce

我的函数为什么不输出列表中的第一项?谢谢。你知道吗

删除函数“bread”,然后将列表中的第一项设置为该参数。你知道吗

我期望函数打印列表中的第一项,但它没有


Tags: ofthe函数列表youronwithbuilder
2条回答
def sandwich_builder(*items):    
    print(f"What type of bread do you want?")
    bread=input("Type of Bread:")
    print(f"Making your sandwich on {bread} bread with the following items:")
    for item in items:
            print(f"-{item}")

sandwich_builder('turkey','bacon','avocado','cheddar','mayonnaise','tomato','lettuce')

只需从函数中删除一个不必要的参数。否则你就得把一个也不传给面包。你知道吗

def sandwich_builder(bread,*items):    
    print(f"What type of bread do you want?")
    bread=input("Type of Bread:")
    print(f"Making your sandwich on {bread} bread with the following items:")
    for item in items:
            print(f"-{item}")

sandwich_builder(None,'turkey','bacon','avocado','cheddar','mayonnaise','tomato','lettuce')

下面的代码行设置了bread='turkey'items = ['bacon', 'avocado', 'cheddar','mayonnaise','tomato','lettuce']。你知道吗

sandwich_builder('turkey','bacon','avocado','cheddar','mayonnaise','tomato','lettuce')

然后使用bread=input("Type of Bread:")重写bread的值。你知道吗

若要修复它,请从函数中删除bread作为参数。你知道吗

相关问题 更多 >