0
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.

p--h
  • 21
  • 5
  • Can you specify desired output and sample input so that it can be understood more clearly ? From what I understand, you need to append values in dictionary till the time user enters Yes ? If they enter No, you should break and then simply print the dictionary ? – mozilla-firefox Jan 20 '20 at 11:12
  • Sure, overall I want to create a program that gives a total of the profit for a user, in the best and worst case scenario. I would like to use the repeating while loop so the user can add as many items as he wants to the dictionary, e.g. 'product A': (100,50), 'product b' : (10, -5) Then at the end I will use max and min to total the good and bad profit outcomes – p--h Jan 20 '20 at 11:15
  • Why not insert inside dictionary in while loop ? – mozilla-firefox Jan 20 '20 at 11:17
  • If I am correct I believe the use of variable names will keep replacing whatever has been appended to the dictionary every time the loop restarts, so I am looking for a way around this – p--h Jan 20 '20 at 11:19

2 Answers2

1
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.

mozilla-firefox
  • 864
  • 1
  • 15
  • 23
1

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.

Renaud
  • 2,709
  • 2
  • 9
  • 24
  • Hey, thanks for reply, the loop code you wrote was great, however, how would I call all the max values in the dictionary for 'good' or 'bad' , as item is not defined – p--h Jan 20 '20 at 12:19
  • Hey, thanks again but my question was much more simple than this, I will try to be more detailed. When calling the dictionary to print the values of the good or bad profit situation with a line such as max(profit_dict[items]['good']))) The name items is not assigned to any key in the dictionary, so I cannot call the value. Ultimately I would like to call all the values for every key and total them. So maybe call the good values and append them to a list? – p--h Jan 20 '20 at 13:27