Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to return variable from one function and use it in another function

I am attempting to return a list of coins from one function to another, then split it and sum the results. I’m trying to return one list from a function, then use it in the next. I put the list in as a positional argument in the main function, but it says it isn’t defined. I have a return statement in the function, so I’m not sure what’s wrong.

import random
import time
import math



CORRECT_LIST = ['Great job!', 'You did it!', 'Right answer.', 'Awesome!']
INCORRECT_LIST = ['Not quite.', 'Try again.', 'Keep trying.', 'Check your math.']


#Controls all other functions
def main():

    choose_coins()
    coin_tokens = choose_coins()

#Choose the number and type of coins
def choose_coins():

    coin_tokens = []

    #List of coins to pick from
    coin_list = ['Penny', 'Nickel', 'Dime', 'Quarter']
    
    #Choose the number of coins
    num_coins = random.randint(2,4)

    #Add chosen coins to a list
    for i in range(num_coins):


        coins_to_be_counted = random.choice(coin_list)

        coin_tokens.append(coins_to_be_counted)

   
    print(coin_tokens)

    return coin_tokens


    


#Add coins using tokens from coin_tokens list
def add_coins(coin_tokens):


    #Iterate through coin_tokens list
    for i in coin_tokens:

        if token == 'Penny':
            coin_total += 1

        elif token == 'Nickel':
            coin_total += 5

        elif token == 'Dime':
            coin_total += 10
    
        elif token == 'Quarter':
            coin_total += 25

        coin_total = 0
        
    print(coin_total)



#Starts the Program
if __name__ == "__main__":
    main()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You have made a lot of minor errors. For instance, using ‘i’ in the for loop but checking condition on a undefined variable ‘token’, calling a function with return value and not assigning a variable for it.

Please check the following code. I have made a changes, it should work now.

import random
import time
import math

CORRECT_LIST = ['Great job!', 'You did it!', 'Right answer.', 'Awesome!']
INCORRECT_LIST = ['Not quite.', 'Try again.', 'Keep trying.', 'Check your math.']


#Controls all other functions
def main():

    coins = choose_coins()
    add_coins(coins)

    
#Choose the number and type of coins
def choose_coins():
    coin_tokens = []
    #List of coins to pick from
    coin_list = ['Penny', 'Nickel', 'Dime', 'Quarter']
    #Choose the number of coins
    num_coins = random.randint(2,4)
    #Add chosen coins to a list
    for i in range(num_coins):
        coins_to_be_counted = random.choice(coin_list)
        coin_tokens.append(coins_to_be_counted)
    print(coin_tokens)
    
    return coin_tokens

#Add coins using tokens from coin_tokens list
def add_coins(coin_tokens):
    coin_total = 0
    for token in coin_tokens:
        if token == 'Penny':
            coin_total += 1
        elif token == 'Nickel':
            coin_total += 5
        elif token == 'Dime':
            coin_total += 10
        elif token == 'Quarter':
            coin_total += 25
            
    print(coin_total)
    


#Starts the Program
if __name__ == "__main__":
    main()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading