I want the user to add new items to the list. When user enters "q", I want the program to quit and then use this list as an input for another function. But I can't break out of the loop as "Q" is added to the list:
base = ["item1", "item2"]
def add_item(item, base=base):
item=item.upper()
base.append(item)
return base
while True:
user_input = add_item(input("Add an item or enter q to finish: "))
if user_input == "Q":
break
else:
print(f"New item added: {user_input}")
print("Added")
Also, how can I later use the final output (final list) as input for another function? I would like to check if the item is already in the list, then do something with this item. I have a general idea that maybe I should use "global" like this:
def check(item):
global base
if item in base:
print("in the list")
else:
print("not here")
Is there a better approach?
because user_input is the return value of your function not the actual input of the program which user typed!
in = input("Add an item or enter q to finish: ")
if in == "Q":
break
else:
user_input = add_item()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.