| | |
def function(): help
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2006
Posts: 75
Reputation:
Solved Threads: 1
I am not sure how to word this question. If I am defining function() to be a user input prompt, does the 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)
name_of_prompt = raw_input("")
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)
def function(): print '''The user inputs their favourite colour, and depending on their input, a different function is called.''' prompt_funct1() def prompt_funct1(): prompt_f1 = raw_input("What is your favourite colour? ").lower() try: if prompt_f1 == "blue" or prompt_f1 == "light blue": explain() else: print '''Just type in 'blue' so this goes smoother! ''' except: pass def explain(): print '''Can I call function prompt_funct1()'s raw input as prompt_funct1 = raw_input("What is your favourite colour") or will it cause confusion when python runs it. Basically, when the user inputs blue or light blue as their favourite colour, will it run function explain()? I hope this makes sense, I confused myself horribly trying to figure out a way to simplify this whole process and code it more professionally, I feel that my code looks sloppy. But in the process of trying to explain my question, my whole mind went numb and dumb, leaving me now very confused as to what I was trying to do in the first place. If any of you can decipher this and/or help me figure this out, please and thank you very much. ''' raw_input("press enter to give me my brain back")
•
•
Join Date: Oct 2006
Posts: 16
Reputation:
Solved Threads: 3
I'm not sure what you're trying to do, but this is how you can define an input statement inside of a function:
edit:
Here's how you would create a response depending on what the user says:
python Syntax (Toggle Plain Text)
def question(q): #Bassiccly, you are setting the variable 'response' to whatever the user types in response = raw_input(q) print "your reponse from inside the function:", response return response 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)
def question(q): #Bassiccly, you are setting the variable 'response' to whatever the user types in response = raw_input(q).lower() print "your reponse from inside the function:", response if response == "red": print "hey! That's my favorite color too!" elif response == "green": print "ew, I hate green" else: print "oh, okay" return response print question("What is your favorite color?")
Last edited by vegaseat; Jun 3rd, 2008 at 8:53 pm. Reason: changed code tags
Three things to note:
1. For printing, you don't need three apostrophes. A normal print statement looks like this:
2. If you have a print statement and want a line after it, you don't need to just print a blank
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
1. For printing, you don't need three apostrophes. A normal print statement looks like this:
python Syntax (Toggle Plain Text)
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)
print "hello" prompt = raw_input("fav color? ").lower() if prompt == "blue": print "Oh! Me, too!" else: print "THAT color? ..."
python Syntax (Toggle Plain Text)
print "hello\n" prompt = raw_input("fav color? ").lower() if prompt == "blue": print "Oh! Me, too!" else: 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
•
•
Join Date: Oct 2006
Posts: 75
Reputation:
Solved Threads: 1
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?
I'm not quite following you. Is this what you mean?
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.
python Syntax (Toggle Plain Text)
def function1(): prompt1 = "blue" return prompt1 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
•
•
Join Date: Oct 2006
Posts: 75
Reputation:
Solved Threads: 1
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.
You use return:
python Syntax (Toggle Plain Text)
def get_color(): color = raw_input("Enter the name of a color: ") # process it color = color.lower() return color pcolor = get_color() print pcolor
drink her pretty
•
•
Join Date: Jun 2008
Posts: 127
Reputation:
Solved Threads: 31
You mean something like that?:
python Syntax (Toggle Plain Text)
functs=['blue','red','yellow'] def explain(): print("just type in the function name like %s"%" or ".join(functs)) def funct_blue(): print("blue called") while True: inp=raw_input("function name: ") try: eval("funct_"+inp)() except NameError: explain()
![]() |
Similar Threads
- Python for Birthday (but having tech difficulties) (Python)
- traceback error? (Python)
- wxPython slider help (Python)
Other Threads in the Python Forum
- Previous Thread: Find, Match and replace..?
- Next Thread: Need help converting temperature, using classes
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti apache approximation array backend beginner binary book builtin calculator chmod code converter countpasswordentry curved dan08 dictionaries dictionary drive dynamic examples excel file filename float format function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion redirect refresh scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame write wxpython






