Search code examples
pythondictionaryprogram-entry-point

main() function will not output value for coin_to_bag


Im getting an error of:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".\file.py", line 113, in <module>
    main()
  File ".\file.py", line 111, in main
    cs_in_the_b = assign_c2b(bs, cs, c2b)
NameError: global name 'c2b' is not defined

When I run my code:

cs = ['quarter','dime','nickel']
bs = ['sm','med','lg']

def assign_c2b(bs, cs):
    '''
    assign_c2b() assigns cs to bs
    specifically c2b{} stores this association between the c and b
    '''
    c2b = {}#'test'}
    print('Here are your bs:\n')
    print(bs)
    print('\n')
    print('Here are your c types:\n')
    print (cs)

    for b in bs:
        c_type = input('Number of this type?    e.g. type 1 for Quarter "["Quarter", "Nickel"]" ')  #e.g. 0.25 * 2
        c_amount = input('Number of this type?  e.g. 15')  #e.g. 0.25 * 2
        for c in cs:
            c2b[b] = [c_type, c_amount]

    print(c2b)

    return (c2b)


def main():
    bs = gather_b()
    cs = gather_c()
    cs_in_the_b = assign_c2b(bs, cs, c2b)

main()

Im simply trying to get my var c2bto display something when called in main() . Perhaps I am totally overlooking the reason, but havent been able to figure this out. Can someone help? Thank you!!


Solution

  • def main():
        bags = gather_bag()
        coins = gather_coin()
        coins_in_the_bag = assign_coin_to_bag(bags, coins, coin_to_bag)
    

    coin_to_bag is never defined when calling assign_coin_to_bag.

    Even if it was defined, assign_coin_to_bag expects 2 arguments (def assign_coin_to_bag(bags, coins) but you are trying to call it with 3.