I would like to create a program that allows you to choose your favorite item. To do this I would like to present the user with two options at a time. I would start of with a list of items and I would need the program to select the first two and then present them to the user. The user would select one and then this item would be added to a results list. The program would iterate over the first list until it has presented all the option and then use the results list as input. At the end there should be only one item in the final list.
I have written some code which presents the options to the user but I cannot think of a way to use the results list as input for the same program.
list1 = ['1','2','3','4']
dict1 = dict(enumerate(list1,0))
def narrow (dict, listing):
for k in list(dict.keys()):
print('dictionary has ' + str(len(list(dict.keys()))) + ' keys')
if len(list(dict.keys())) == 0:
break
elif len(list(dict.keys())) != 0:
if len(list(dict.keys())) != 3:
choice = input (dict[k] + ' or ' + dict[k+1] + ' ')
if choice == 'a':
listing.append (dict[k])
print(listing)
dict.pop(k)
dict.pop(k+1)
print(dict)
elif choice == 'b':
listing.append (dict[k+1])
print(listing)
dict.pop(k)
dict.pop(k+1)
print(dict)
narrow(dict, listing)
return(listing)
elif len(list(dict.keys())) == 3:
choice = input (dict[k] + ' or ' + dict[k+1] + ' or ' + dict[k+2] + ' ')
if choice == 'a':
listing.append (dict[k])
print(listing)
dict.pop(k)
dict.pop(k+1)
dict.pop(k+2)
print(dict)
elif choice == 'b':
listing.append (dict[k+1])
print(listing)
dict.pop(k)
dict.pop(k+1)
dict.pop(k+2)
print(dict)
elif choice == 'c':
listing.append (dict[k+2])
print(listing)
dict.pop(k)
dict.pop(k+1)
dict.pop(k+2)
print(dict)
narrow(dict, listing)
return(listing)
list2 = []
narrow(dict1,list2)
Rather than modify the list you pass in, I recommend taking a copy and working with it. If the user wanted to then update the original they could decide to do that themselves.
Note this is conceptually similar to the answer by @mathias-r-jessen
import random
##-----------------
## Given a list of values, return the favored one.
##-----------------
def narrow(values):
if not values:
return None
values = list(set(values)) ## take a copy and remove duplicates
while len(values) > 1:
choices = random.sample(values, 2)
choice = input(f"Do you prefer \"{choices[0]}\" over \"{choices[1]}\" (y|n)? ")
item_to_remove = choices[1] if choice == "y" else choices[0]
values.remove(item_to_remove)
return values[0]
##-----------------
all_values = ["1","2","3","4"]
favored_value = narrow(all_values)
print(f"Out of all the values in {all_values}, your favorite was '{favored_value}'")