0

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()
3
  • Could you paste the specific error message? Commented Feb 7, 2022 at 15:05
  • A return statement doesn't magically make a variable name available in the scope of the caller of the function; indeed, you can return things other than a variable, such as a literal or expression. You have to explicitly make use of the function's value at the point of the call - something like somevar = choose_coins() for example. Commented Feb 7, 2022 at 15:06
  • @BenB It doesn't make sense to call split() on a list. The initial lines of coin_tokens.split() should simply be erased. Commented Feb 7, 2022 at 15:10

6 Answers 6

1

You're returning a value, but you're not assigning it to a variable.

my_coins = choose_coins()
add_coins(my_coins)
Sign up to request clarification or add additional context in comments.

Comments

0

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()

Comments

0

You could try doing:

def main():
    add_coins(choose_coins())

instead of

def main():

    choose_coins()
    add_coins(coin_tokens)

In your version the functions aren't connected and you're probably getting an error like NameError: name 'coin_tokens' is not defined

Comments

0

Your return statement needs to be fixed. return [coin_tokens] returns a list with your list inside it. So this:

    return[coin_tokens]

should be:

    return coin_tokens

Then, assign the return value:

coin_tokens = choose_coins()

1 Comment

I edited the post with the updated code, thanks for the assist.
0

There are multiple problems with your code. The syntax to call a function is as follows:

return_value = function_name(arguments)

You are calling choose_coins() but not storing the return value:

def main():

    coin_tokens = choose_coins()
    add_coins(coin_tokens)

Or even:

 add_coins(choose_coins())

When you write:

return[coin_tokens]

It is wrapping your coin_tokens which is already a list into another list. Change it to:

return coin_tokens

You do not need this line; it has no effect:

coin_tokens.split()
print(coin_tokens)

You should use token instead of i in the for loop. And have not initialized coin_total to 0 before the loop:

def add_coins(coin_tokens):
    
    print(coin_tokens)
    # Initialize to 0
    coin_total = 0

    #Iterate through coin_tokens list
    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)

And it would be a better idea to return coin_total:

return coin_total

And use it like this in main:

coin_tokens = choose_coins()
total = add_coins(coin_tokens)
print(f"Total is {total}")

Comments

0

You might the following rewrite of your code to be interesting.

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.']
COIN_LIST = ['Penny', 'Nickel', 'Dime', 'Quarter']
VALUE = {'Penny':1,'Nickel':5,'Dime':10,'Quarter':25}

def main():
    coin_tokens = choose_coins()
    add_coins(coin_tokens)

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

    #List of coins to pick from
    global COIN_LIST

    #Choose the number of coins
    num_coins = random.randint(2,4)
    coin_tokens = random.choices(COIN_LIST,k=num_coins)
    
    print(coin_tokens)
    return coin_tokens
    


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

    print(coin_tokens)
    global VALUE 
    coin_total = sum(VALUE[coin] for coin in coin_tokens)
    
    print(coin_total)


main()

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

You could further shorten the code to the following.

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.'] 
VALUE = {'Penny':1,'Nickel':5,'Dime':10,'Quarter':25}

def main():
    coin_tokens = choose_coins()
    print(coin_tokens)
    total = add_coins(coin_tokens)
    print(total)

#Choose the number and type of coins
def choose_coins():
    return random.choices(list(VALUE),k=random.randint(2,4))

#Add coins using tokens from coin_tokens list
def add_coins(coin_tokens):
    return sum(VALUE[coin] for coin in coin_tokens)

Comments

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.