Reputation: 28
I need to write a program in which user is asked: to select 1 (and only 1) team amongst 3 teams and to select 1 or several products amongst a longer product list.
My code works fine with 1 product only.
Any hint? (I found this useful post related, but not really documented on multiple selection)
Here is my program (Python 3.7)
from itertools import chain, repeat
# User select 1 team, uppercase
teams = {'OTEAM', 'ZTEAM', 'DREAMTEAM'}
prompts = chain(["Enter 1 team: "], repeat("No such team? Try again: "))
replies = map(input, prompts)
valid_response = next(filter(teams.__contains__, replies))
# Show team selected
print(f"you want to configure {valid_response}")
# User select 1 or several product,
products = {'Aprod', 'Bprod', 'Cprod'}
prompts_prod = chain(["Enter product(s): "], repeat("No such product, try again: "))
replies_prod = map(input, prompts_prod)
valid_response_prod = next(filter(products.__contains__, replies_prod))
print(f"---Result --- \nin {valid_response} you want to configure \n{valid_response_prod}")
Upvotes: 0
Views: 442
Reputation: 4251
If the users provide a product list in a space-separated format then change your code to accept that, split it into a set and use set operations to find the products they have entered that are valid, and those that are invalid.
products = {'Aprod', 'Bprod', 'Cprod'}
requested_products = set(input("Enter Space Separated Product List").split())
selected_products = requested_products.intersection(products)
invalid_products = requested_products-selected_products
#Do something with invalid products e.g.
print(f'Accepted products {",".join([p for p in selected_products])}')
print(f'Invalid products {",".join([p for p in invalid_products])}')
Up to you at that point whether you error, loop until there are non invalid, etc.
Upvotes: 1