我构建了一个将参数接受到列表中的函数,然后只需要使用该函数即可打印出三明治订单。
我尝试仅使用打印功能并将功能名称用作要打印的内容。
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.
答案 0 :(得分:0)
您可以尝试以下代码:
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.
答案 1 :(得分:0)
您可以通过以下方式进行操作:
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)