def function(): help

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 75
Reputation: mruane is an unknown quantity at this point 
Solved Threads: 1
mruane mruane is offline Offline
Junior Poster in Training

def function(): help

 
0
  #1
Oct 28th, 2006
I am not sure how to word this question. If I am defining function() to be a user input prompt, does the
  1. name_of_prompt = raw_input("")
name of prompt equal the name of fucntion or could it be
prompt_funct1? If so, when using if...elif statements to call on other functions providing prompt_funct1 == something specific, how do I do that. This is my understanding of the area...
  1. def function():
  2. print '''The user inputs their favourite colour, and depending on their
  3. input, a different function is called.'''
  4. print
  5. prompt_funct1()
  6. def prompt_funct1():
  7. prompt_f1 = raw_input("What is your favourite colour? ").lower()
  8. try:
  9. if prompt_f1 == "blue" or prompt_f1 == "light blue":
  10. explain()
  11. else:
  12. print '''Just type in 'blue' so this goes smoother! '''
  13. except:
  14. pass
  15. def explain():
  16. print '''Can I call function prompt_funct1()'s raw input as
  17. prompt_funct1 = raw_input("What is your favourite colour") or will
  18. it cause confusion when python runs it. Basically, when the user inputs
  19. blue or light blue as their favourite colour, will it run function explain()?
  20. I hope this makes sense, I confused myself horribly trying to figure out a way
  21. to simplify this whole process and code it more professionally, I feel that my
  22. code looks sloppy. But in the process of trying to explain my question, my whole
  23. mind went numb and dumb, leaving me now very confused as to what I was trying to do
  24. in the first place. If any of you can decipher this and/or help me figure this out,
  25. please and thank you very much. '''
  26. print
  27. raw_input("press enter to give me my brain back")
  28.  
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 16
Reputation: Zonr_0 is an unknown quantity at this point 
Solved Threads: 3
Zonr_0 Zonr_0 is offline Offline
Newbie Poster

Re: def function(): help

 
0
  #2
Oct 28th, 2006
I'm not sure what you're trying to do, but this is how you can define an input statement inside of a function:

  1. def question(q):
  2. #Bassiccly, you are setting the variable 'response' to whatever the user types in
  3. response = raw_input(q)
  4. print "your reponse from inside the function:", response
  5. return response
  6.  
  7. print question("What is your favorite color?")

edit:
Here's how you would create a response depending on what the user says:
  1. def question(q):
  2. #Bassiccly, you are setting the variable 'response' to whatever the user types in
  3. response = raw_input(q).lower()
  4. print "your reponse from inside the function:", response
  5. if response == "red":
  6. print "hey! That's my favorite color too!"
  7. elif response == "green":
  8. print "ew, I hate green"
  9. else:
  10. print "oh, okay"
  11. return response
  12.  
  13. print question("What is your favorite color?")
Last edited by vegaseat; Jun 3rd, 2008 at 8:53 pm. Reason: changed code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: def function(): help

 
0
  #3
Oct 28th, 2006
Three things to note:

1. For printing, you don't need three apostrophes. A normal print statement looks like this:

  1. print "Hello World"


2. If you have a print statement and want a line after it, you don't need to just print a blank print statement. Y ou can add \n at the end of your first print statement. The following two blocks of code have identical output:

  1. print "hello"
  2. print
  3. prompt = raw_input("fav color? ").lower()
  4. if prompt == "blue":
  5. print "Oh! Me, too!"
  6. else:
  7. print "THAT color? ..."

  1. print "hello\n"
  2. prompt = raw_input("fav color? ").lower()
  3. if prompt == "blue":
  4. print "Oh! Me, too!"
  5. else:
  6. print "THAT color? ..."

3. raw_input() is an input function. You can assign it to any variable and the input from the user will be stored into that variable. So, in your prompt_funct1(), you're gettting an input from the user and storing it to the variable prompt_f1. Let's say I type in "blue" when it gives me a prompt. Afterwards, prompt_f1 = "blue". Remember... without doing extra code, you cannot reference variables in one function in another function. Meaning...if you create a new function, you can't immediately start using the prompt_f1 variable and expect to get "blue."

Hope that helped.
-Mouche
Last edited by vegaseat; Jun 3rd, 2008 at 8:55 pm. Reason: changed code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 75
Reputation: mruane is an unknown quantity at this point 
Solved Threads: 1
mruane mruane is offline Offline
Junior Poster in Training

Re: def function(): help

 
0
  #4
Oct 29th, 2006
Thanks, I have a better understanding now. My main problem was that I spent to much time working on a program and went past my brains ability to concentrate, therefore complicating any simple task I tried to accomplish. So basically, if I define that def function1() has prompt1 == blue, then prompt1 = blue, not function1()? That would mean that function1() = a call for prompt1 to = blue, or other user input, correct?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: def function(): help

 
0
  #5
Oct 29th, 2006
I'm not quite following you. Is this what you mean?

  1. def function1():
  2. prompt1 = "blue"
  3. return prompt1
  4.  
  5. color = function1() # color now equals "blue"

This is an odd, useless function, but that's what I'm getting out of your text. Sorry if I'm not understanding. A function can have a return value, which is the value given if you assign a function to a variable.
Last edited by vegaseat; Jun 3rd, 2008 at 8:55 pm. Reason: changed code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 75
Reputation: mruane is an unknown quantity at this point 
Solved Threads: 1
mruane mruane is offline Offline
Junior Poster in Training

Re: def function(): help

 
0
  #6
Oct 30th, 2006
Thanks guys, I figured it out. I was somehow confused about the function. I was thinking does prompt1 equal the same as the function I am defining? I just got a little confused. In another thread somebody said that in denaystart() it shoud be "if denaystart == "denay" or denaystart == "denay wiles", while denaystart() was the function I was defining and I named the raw_input prompt something else (say prompt1). I thought it should have been "if prompt1 == "denay" and that he was telling me otherwise. I basically misunderstood what I was being told and instead of clarifying it with the author I just let myself get confused and ask a bunch of stupid questions. Sorry for the waste of time guys. But thanks none the less.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 5
Reputation: caribedude is an unknown quantity at this point 
Solved Threads: 0
caribedude caribedude is offline Offline
Newbie Poster

Re: def function(): help

 
0
  #7
Jun 2nd, 2008
O.k. I understood everything that has gone on here so far, but what I want to know is how to extract a variable from a def. For example, if I wanted to use the response variable for something outside the def, how would I extract it?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: def function(): help

 
0
  #8
Jun 3rd, 2008
You use return:
  1. def get_color():
  2. color = raw_input("Enter the name of a color: ")
  3. # process it
  4. color = color.lower()
  5. return color
  6.  
  7. pcolor = get_color()
  8. print pcolor
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 127
Reputation: slate is an unknown quantity at this point 
Solved Threads: 31
slate slate is offline Offline
Junior Poster

Re: def function(): help

 
0
  #9
Jun 3rd, 2008
You mean something like that?:

  1. functs=['blue','red','yellow']
  2. def explain():
  3. print("just type in the function name like %s"%" or ".join(functs))
  4.  
  5. def funct_blue():
  6. print("blue called")
  7.  
  8. while True:
  9. inp=raw_input("function name: ")
  10. try:
  11. eval("funct_"+inp)()
  12. except NameError:
  13. explain()
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC