![]() |
moving variables to and fro functions - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: moving variables to and fro functions (/thread-11743.html) |
moving variables to and fro functions - zukochew - Jul-24-2018 Hello all, I am a beginner to python. I am unable to transfer the value of variables() to dispense. I was instructed to put value into the bracket function of dispense() . My codes are below. Any help would be appreciated. def variables(): solution = input("Please choose Solution") t= input("time taken") def dispense(solution, t): if solution == 1: print("solution",solution,"time taken:", t) elif solution == 2: print("solution",solution,"time taken",t) else: print("other solution") variables() dispense(solution,t) RE: moving variables to and fro functions - buran - Jul-24-2018 you should use return statement in order to get something out of the function def user_input(): solution = input("Please choose Solution") t = input("time taken") return solution, t # your other code here solution, t = user_input() # it is not mandatory to use same variable names here dispense(solution=solution,t=t) # pass what is returned from user_input as arguments when call dispense # as an alternative to last two lines dispense(*user_input())Note that solution and t variables are in fact different objects in the user_input, in the dispense function and in the global scope even if they have same names. That is related to the scope of the variables however there are also other problems in your code. input() will return string (str). However in your dispense function you compare solution with int. So it will always be False and your dispense function will always print 'other solution', how to fix it def dispense(solution, t): if solution == '1': # here it is now str, not int print("solution {} time taken: {}".format(solution, t)) # using str.format() method elif solution == '2': # here it is now str, not int print("solution {} time taken: {}".format(solution, t)) else: print("other solution") def dispense(solution, t): if solution in ('1', '2'): print(f"solution {solution} time taken: {t}") # here using f-strings available from 3.6+ else: print("other solution") RE: moving variables to and fro functions - zukochew - Jul-24-2018 Hello Buran, Thanks for your help. Sorry to interrupt you as I have another question in mind. What does the asterisk do in dispense(*user_input()) Regards, Zuko RE: moving variables to and fro functions - buran - Jul-24-2018 first of all, let say that user_input function in my example returns tuple. i.e. it returns single object, container that has 2 elements in it. dispense function expects two arguments. You need to unpack the container, i.e. sort of 'extract' the elements in the container in order to supply them to dispense function. You can do that beforehand - assign each element to a variable and then pass these variables to dispense function. Or you can skip that step and unpack the container in the dispense function call. Note that if you unpack mapping container like dict (i.e. where you have key:value pairs) you need to use ** def dispense(solution, t): if solution in ('1', '2'): print("solution {} time taken: {}".format(solution, t)) else: print("other solution") my_variables = ('1', 10) dispense(*my_variables) my_variables = {'solution':'2', 't':40} dispense(**my_variables) also, you may want to look at https://medium.com/understand-the-python/understanding-the-asterisk-of-python-8b9daaa4a558just a note, make sure you understand positional and keyword arguments to functions RE: moving variables to and fro functions - zukochew - Jul-25-2018 Hello Buran, Thanks for your help! Regards, Zuko |