Как распечатать аргументы из функции отдельно? - PullRequest
0 голосов
/ 11 ноября 2019

Я построил функцию, которая принимает аргументы в список, а затем мне просто нужно использовать функцию для распечатки сэндвич-заказов.

Я пытался просто использовать функцию печати и использовать имя функции в качестве вещи, которую яЯ пытаюсь распечатать.

def sandwich_items(*items):
    sandwich_items = []
    sandwich_items.append(items)
    print("\nEnter what you want on your sandwich.")
    for item in items:
        print(" ...Putting " + item + " on your sandwich.")
    print("Your sandwich is ready!")

sandwich_items('tomatoes', 'lettuce', 'ham', 'salami')
sandwich_items('peanut butter', 'jelly')
sandwich_items('turkey breast', 'chicken breast', 'cheese')

#This is what I tried
print('I have made your ' + str(sandwich_items) + 'sandwich.')

Когда я попытался распечатать его, я получил сообщение об ошибке:

 I have made your <function sandwich_items at 0x038295D0>sandwich.

Ответы [ 2 ]

0 голосов
/ 11 ноября 2019

Вы можете сделать следующим образом:

def add_items(lst):

     my_lst=[]
     print("\nEnter what you want on your sandwich.")
     for i in lst :
          my_lst.append(i)
          print(" ...Putting " + i + " on your sandwich.")
     print("Your sandwich is ready!")


lst = ['tomatoes', 'lettuce', 'ham', 'salami']

add_items(lst)
0 голосов
/ 11 ноября 2019

Вы можете попробовать этот код:

def sandwich_items(*items):

  sandwich_items = []
  sandwich_items.append(items)
  print("\nEnter what you want on your sandwich.")
  for item in items:
       print(" ...Putting " + item + " on your sandwich.")

  print("Your sandwich is ready!")
  return ",".join(items )

sandw1 = sandwich_items('tomatoes', 'lettuce', 'ham', 'salami')
sandw2 = sandwich_items('peanut butter', 'jelly')
sandw3 = sandwich_items('turkey breast', 'chicken breast', 'cheese')

#This is what I tried
print('I have made your ' + sandw1 + ' sandwich.')

Вывод:

Enter what you want on your sandwich.
...Putting tomatoes on your sandwich.
...Putting lettuce on your sandwich.
...Putting ham on your sandwich.
...Putting salami on your sandwich.
Your sandwich is ready!

Enter what you want on your sandwich.
...Putting peanut butter on your sandwich.
...Putting jelly on your sandwich.
Your sandwich is ready!

Enter what you want on your sandwich.
...Putting turkey breast on your sandwich.
...Putting chicken breast on your sandwich.
...Putting cheese on your sandwich.
Your sandwich is ready!

I have made your tomatoes,lettuce,ham,salami sandwich.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...