0
{'sm': ['q', 0.25, 1000], 'Bug Out Bag': ['q', 0.25, 100000000]}

Calculate for total amount, here's my code:

single_or_all_total = input('\nDo you want 1 bag or all the bags? (type: 1 for 1 bag, 2 for all bags)\n')
#calculate for a single bag
if int(single_or_all_total) == 1:
    single_bag_total = input('\nWhich bag? (e.g. type: small for the "small" bag)\n')
    if single_bag_total in coins_in_the_bag:
        total = coins_in_the_bag[single_bag_total][1] * coins_in_the_bag[single_bag_total][2]
        print('The total for bag {} was {}'.format(single_bag_total, total))
        #print(total)
    elif single_bag_total not in coins_in_the_bag:
        print('Sorry, this was not a valid bag. Please re-run file.')
    else:
        print('An error occurred, please re-run script.')

#calculate total for all bags
elif single_or_all_total == 2:
    total=0
    single_bag_total=0
    for coin in coins_in_the_bag:
        total += coins_in_the_bag[single_bag_total][1] * coins_in_the_bag[single_bag_total][2]
    print(total)

the problem is, when I run this, it returns nothing.

I can make my code hit the if int(single_or_all_total) == 1:, but if i input a 2 it doesnt crash at all, it just doesnt return anything.

I'm simply trying to loop through coins_in_the_bag, while adding each value to the total var: ( coins_in_the_bag[single_bag_total][1] * coins_in_the_bag[single_bag_total][2] ) but it is not outputting anything.

What am i doing wrong here?

Updated Error

    total += coins_in_the_bag[single_bag_total][1] * coins_in_the_bag[single_bag
_total][2]
UnboundLocalError: local variable 'total' referenced before assignment

1 Answer 1

2

You never hit the loop you suspected.

From your use of print as a function (and your conversions of single_or_all_total), I'm going to deduce that you are using Python 3.

Then, input returns a simple string, just like raw_input in Python 2.

You compare int(single_or_all_total) to 1 (which is fine, and works), and then single_or_all_total to 2. Note that this second version misses the conversion to a number - the string in single_or_all_total is never going to be equal to 2, only "2".

Instead, try elif int(single_or_all_total) == 2 or elif single_or_all_total == "2"

Sign up to request clarification or add additional context in comments.

4 Comments

Good catch, although, this doesnt quite fix it all. Can you look at the updated error above, which still has to do with this "total" var.
total is not yet defined. Try putting total = 0 before the loop
Please see updated, now giving me. total += coins_in_the_bag[single_bag_total][1] * coins_in_the_bag[single_bag _total][2] KeyError: 0
I don't know your exact data, so this is mostly guessing, but from the top branch of your if statement, I'm going to suggest you try using coin instead of single_bag_total here (because coin iterates over the same values as single_bag_total does above)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.