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()
returnstatement doesn't magically make a variable name available in the scope of the caller of the function; indeed, you canreturnthings 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 likesomevar = choose_coins()for example.split()on a list. The initial lines ofcoin_tokens.split()should simply be erased.