简体   繁体   中英

How can I use a while loop to append a dictionary with an arbitrary number of keys and multiple values from user inputs

print('Scenario Analysis')

profit_dict = {}

while True:
    item1= input ('What is the item: ')   
    price_good= int (input('What is the expected profit of {}, in the best case scenario: '.format(item1)))
    price_bad = int( input ('What is the expected profit of {}, in the worst case scenario: '.format(item1)))
    user_choice= input('Do you have anymore items: ')
    if user_choice in ['Yes','yes','y','Y']:
            pass
    else:
            break

profit_dict[item1]=price_good,price_bad

#EVERYTHING FROM HERE ON IS NOT RELEVANT DIRECTLY TO QUESTION BUT PROVIDES CONTEXT

print('This is your best profit outcome: ')
for values in good_dict.values():
    total_list=(max(values))
    print(total_list)

print('This is your worst profit outcome: ')

I understand the use of variables will keep replacing the dictionary but it's the best way I can show what I'm aiming for. Possibly the use of a function instead of a while loop may help, but I am unsure.

Thanks in advance for any replies.

profit_dict = {}

while True:
    item1= input ('What is the item: ')   
    price_good= int (input('What is the expected profit of {}, in the best case scenario: '.format(item1)))
    price_bad = int( input ('What is the expected profit of {}, in the worst case scenario: '.format(item1)))
    user_choice= input('Do you have anymore items: ')
    profit_dict[item1]=[price_good,price_bad]    
    if user_choice in ['Yes','yes','y','Y']:
            continue
    else:
            break

print(profit_dict)

Is this what you are looking for ? This just keeps on adding in dictionary till the user types Yes.

Is something like that is according to your expectations:

print('Scenario Analysis')

profit_dict = {}

while True:
   item1= input ('What is the item: ')
   price_good= int (input('What is the expected profit of {}, in the best case scenario: '.format(item1)))
   price_bad = int( input ('What is the expected profit of {}, in the worst case scenario: '.format(item1)))

   if item1 in profit_dict.keys():
       profit_dict[item1]['good'].append(price_good)
       profit_dict[item1]['bad'].append(price_bad)

   else:
       profit_dict[item1]={'good':[price_good],'bad':[price_bad]}

   user_choice= input('Do you have anymore items: ')
   if user_choice in ['Yes','yes','y','Y']:
        pass
   else:
        break

=> You will result with a dictionnary of dictionary: {'Item1':{'good':[1,2,3], 'bad':[0,0,1]}, 'Item2':{'good':[10,20,30], 'bad':[1,2,3]}, ,...,'ItemX':{'good':[10,20,30], 'bad':[1,2,3]}}

And after you can try to call the print with something like:

print(f'This is your best profit outcome: {max(profit_dict[item]['good'])}')
print(f'This is your worst profit outcome: {min(profit_dict[item]['bad'])}')

EDIT: Not sure do understand you comment: if you are looking for a specific item:

item='item1'
print(f'This is your best profit outcome: {max(profit_dict[item]['good'])}')

if item is not known you can explore the dictionnary by iteration:

for key, value in profit_dict.items():
    for key2, value2 in value.items():
        print(f"This is your best profit outcome for {key}:{max(profit_dict[item]['good'])}")

it will print you: This is your best profit outcome for Item1:3 This is your best profit outcome for Item2:30 ... This is your best profit outcome for ItemX:30

And if you want to know the item with the bigger 'good' this solution is not the best but you can still do an iteration:

previous_best_value=0
for key, value in profit_dict.items():
    for key2, value2 in value.items():
        best_value=max(profit_dict[key]['good'])
        if best_value>previous_best_value:
            previous_best_value=best_value
            best_item=key

print(f"The item:{key} has the best outcome {previous_best_value}")

Hope you will find what you expected.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM