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 = 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...

def function():
    print '''The user inputs their favourite colour, and depending on their
    input, a different function is called.'''
    print
    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. '''
    print
    raw_input("press enter to give me my brain back")

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

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:

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?")
Member Avatar for Mouche

Three things to note:

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

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:

print "hello"
print
prompt = raw_input("fav color? ").lower()
if prompt == "blue":
    print "Oh! Me, too!"
else:
    print "THAT color? ..."
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

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?

Member Avatar for Mouche

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

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.

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.

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?

You use return:

def get_color():
    color = raw_input("Enter the name of a color: ")
    # process it
    color = color.lower()
    return color

pcolor = get_color()
print pcolor

You mean something like that?:

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()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.