Python Forum
Error in using the output of one function in another function (beginner)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in using the output of one function in another function (beginner)
#1
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.
Reply
#2
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() )
Reply
#3
Hello! You have to call the function.

if choice() == "1":
    print("ONE")
else:
    print("TWO")
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Perfect thanks alot!!

Why is it allowed to write:

b) my_choice=choice()

But not

b) choice() = my_choice??
Reply
#5
Because you assign an object to a variable name - my_choice = choice() Call() is an object.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
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
Reply
#7
Thanks for the help guys! 

I definitely learned some usefull stuff by reading your comments.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 684 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  Error with output djprasanna 1 555 Nov-28-2023, 06:40 PM
Last Post: deanhystad
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 604 Nov-23-2023, 02:53 PM
Last Post: rob101
  problem in output of a function akbarza 9 1,216 Sep-29-2023, 11:13 AM
Last Post: snippsat
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,402 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  undefined function error JonWayn 5 1,459 Sep-11-2022, 03:38 AM
Last Post: JonWayn
  How to print the output of a defined function bshoushtarian 4 1,323 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  Beginner Python Error ianmac88 4 1,317 Sep-05-2022, 12:30 PM
Last Post: jefsummers
  Error in find pearson correlation function erneelgupta 1 1,878 Mar-01-2022, 03:41 PM
Last Post: stevendaprano
  how can I solve fsolve function error? troddydeeneeeeeey 3 2,455 Oct-14-2021, 07:07 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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