0

I'm trying to run my function: show_total() , but I get this error:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File ".\file.py", in <module>
 main()
 File ".\file.py", in main
 total_money = show_total(bags, coins, coin_to_bag)
NameError: global name 'coin_to_bag' is not defined

my code looks like:

def assign_coin_to_bag(bags, coins):
 coin_to_bag = {}
 print(bags)
 print('\n')
 print (coins)
 print('\n')
 for bag in bags:
 print('For bag: ' + bag )
 coin_type = input('\nWhich coin do you want to put in this bag? ') #e.g. 0.25 * 2
 coin_amount = input('\nNumber of this type? ') #e.g. 0.25 * 2
 mattress_status = 'stuffed'
 for coin in coins:
 coin_to_bag[bag] = [coin_type, coin_amount, mattress_status]
 print(coin_to_bag)
 return (coin_to_bag)
def main():
 bags = gather_bag()
 coins = gather_coin()
 coins_in_the_bag = assign_coin_to_bag(bags, coins)
 total_money = show_total(bags, coins, coin_to_bag)
main()

Thank you for your help!

Adam Smith
54.6k13 gold badges84 silver badges120 bronze badges
asked Dec 7, 2015 at 16:20
6
  • Please post your main function as well. It seems like the coin_to_bag variable does not exist at the time of calling show_total from main Commented Dec 7, 2015 at 16:22
  • 1
    It doesn't help to see the function body when the error occurs where you're passing in the argument. Commented Dec 7, 2015 at 16:23
  • Updated it, its live. Commented Dec 7, 2015 at 16:24
  • And where did you think you defined coin_to_bag? Commented Dec 7, 2015 at 16:30
  • in show_total() your program will crash if user enters *. You cannot change * to int. if int(single_or_all_total) == 1: Commented Dec 7, 2015 at 16:30

1 Answer 1

3

coin_to_bag is a defined in the scope of assign_coin_to_bag, and is not accessible in main. You caught the return value of assign_coin_to_bag in coins_in_the_bag, and need to use that variable in your call to show_total.

There's a common error new programmers make, which is thinking the name of a variable needs to be the same wherever it's used, even across methods. In fact, the name of the variable means absolutely nothing to the computer. Good names are only there for humans.

As an exercise, we used to have students trace code like this:

def foo(one, three, two):
 print "%s %s %s" % (one, two, three)
def bar():
 return 1, 2, 4
three, two, one = bar()
foo(two, one, three)

Figure out what gets printed, and that will be good practice to break the variable naming habit.

answered Dec 7, 2015 at 16:33

4 Comments

The same way you've been using variables everywhere else. show_total(bags, coins, coins_in_the_bag).
Does both the declaration need to change to: def show_total(bags,coins,coins_in_the_bag): ??? and the call: total_money = show_total(bags, coins, coin_to_bag,coins_in_the_bag) ?
No. Your confusion here is that you think a variable must be named the same thing everywhere in the program - that would make writing and using libraries impossible. The name of the variable you pass does not have to be the same as the name of the argument it's received in, nor the same as the return value it caught.
Great catch, and thanks for helping me to understand my error.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.