Python Forum

Full Version: Error in using the output of one function in another function (beginner)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello forum,

Im very much a beginner in programming. I have been spening the last couple of hours to find a solution for this simple problem. So now was the time to join the forum and get help.

I have simplified the problem area of my code in this small code. 

I want the user to make a choice in one definition and then have another definition respond to that choice. 
type in "1" should give "ONE"
type in 2 should give "TWO"

However, if I run the following code and type in "1" I will not get "ONE" as a response but "TWO" . What am I doing wrong? (why is "1" in one definition not equal to "1" in the other)?


def choice():
    choice="0"
    while choice !="1" and choice !="2":
        choice=input ("Type 1 og 2  ")
    return (choice)

def response(choice):
    if choice =="1":
        print("ONE")
    else:
        print("TWO")

choice()
response(choice)
I know I am missing something basic but I just can't figure out waht it is.
You are missing to store your choice to a variable (or use it directly). Your choice is returned by choice(), but you dont use its return value, you just call your response with its parameter being function choice, not returning value.

Use either
my_choice = choice()
response(my_choice)
or without using auxillary variable - return value of choice is passed directaly as a parameter to response:
response( choice() )
Hello! You have to call the function.

if choice() == "1":
    print("ONE")
else:
    print("TWO")
Perfect thanks alot!!

Why is it allowed to write:

b) my_choice=choice()

But not

b) choice() = my_choice??
Because you assign an object to a variable name - my_choice = choice() Call() is an object.
Hmm, both of these statements are "assignment" statements - the right side of statement is evaluated and assigned to the left side.

  1. you can put returned value of a function call into a variable
  2. but you cant put some variable into function call
Is important to differentiate between  = - assignment (not equality) operator and  == - comparison/equality
Thanks for the help guys! 

I definitely learned some usefull stuff by reading your comments.