Python Forum
moving variables to and fro functions
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
moving variables to and fro functions
#1
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)    
Reply
#2
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")
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
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)
Output:
solution 1 time taken: 10 solution 2 time taken: 40
also, you may want to look at https://medium.com/understand-the-python...9daaa4a558
just a note, make sure you understand positional and keyword arguments to functions
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Hello Buran,
Thanks for your help!
Regards,
Zuko
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020