943,982 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 6154
  • Python RSS
Oct 28th, 2006
0

def function(): help

Expand Post »
I am not sure how to word this question. If I am defining function() to be a user input prompt, does the
Python Syntax (Toggle Plain Text)
  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...
Python Syntax (Toggle Plain Text)
  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.  
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
mruane is offline Offline
76 posts
since Oct 2006
Oct 28th, 2006
0

Re: def function(): help

I'm not sure what you're trying to do, but this is how you can define an input statement inside of a function:

python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 3
Newbie Poster
Zonr_0 is offline Offline
16 posts
since Oct 2006
Oct 28th, 2006
0

Re: def function(): help

Three things to note:

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

python Syntax (Toggle Plain Text)
  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:

python Syntax (Toggle Plain Text)
  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? ..."

python Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Oct 29th, 2006
0

Re: def function(): help

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?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
mruane is offline Offline
76 posts
since Oct 2006
Oct 29th, 2006
0

Re: def function(): help

I'm not quite following you. Is this what you mean?

python Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Oct 30th, 2006
0

Re: def function(): help

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
mruane is offline Offline
76 posts
since Oct 2006
Jun 2nd, 2008
0

Re: def function(): help

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
caribedude is offline Offline
24 posts
since Jun 2008
Jun 3rd, 2008
0

Re: def function(): help

You use return:
python Syntax (Toggle Plain Text)
  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
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Jun 3rd, 2008
0

Re: def function(): help

You mean something like that?:

python Syntax (Toggle Plain Text)
  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()
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Find, Match and replace..?
Next Thread in Python Forum Timeline: Need help converting temperature, using classes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC