1

I was experimenting with what I have learned so far and I wanted to create something that is interactive, using raw_input().

What I wanted to do was creating a function that will create a conversation which will go different directions based on input. However, I was unable to figure out how to make a function accept the raw_input as its argument.

Here is the code that I've written;

drink = raw_input("Coffee or Tea?")

def drinktype(drink):
    if drink == "Coffee":
    #I WANT TO INSERT A CODE HERE THAT WILL CALL THE FUNCTION coffee(x)
    elif drink == "Tea":
        print "Here is your tea."
    else:
        print "Sorry."

x = raw_input("Americano or Latte?")

def coffee(x):
    if x == "Americano":
        return "Here it is."    
    elif x == "Latte":
        return "Here is your latte."
    else:
        return "We do not have that, sorry."
4
  • 2
    As is, this code does not work. Please make sure that you are at least abiding by Python's syntax and also ensure you are properly indented. Indentation is important in Python. Commented Jul 29, 2016 at 15:20
  • 3
    You don't seem to ever call your functions. Commented Jul 29, 2016 at 15:22
  • I changed indentations, thanks for reminding but I was careless while pasting the code here, sorry. How do I call the functions? Commented Jul 29, 2016 at 15:23
  • @sonooob - Along with indentation, please restructure your code. Define all functions first then main function. Run it and please paste output/error. . Cheers !!! Commented Jul 29, 2016 at 15:27

3 Answers 3

1

The request for americano or latte is something you only need to do if coffee is requested; it is irrelevant if the user requests tea. Once that is moved under the Coffee case, you can simply pass the returned value to your call to coffee(). The return value also needs to be printed.

def drinktype(drink):
    if drink == "Coffee":
        kind = raw_input("Americano or Latte?")
        print coffee(kind)
    elif drink == "Tea":
        print "Here is your tea."
    else:
        print "Sorry."

def coffee(x)
    if x == "Americano":
        return "Here it is."    
    elif x == "Latte":
        return "Here is your latte."
    else:
        return "We do not have that, sorry."

drink = raw_input("Coffee or Tea?")
drinktype(drink)
Sign up to request clarification or add additional context in comments.

Comments

0

Are you looking for something like this?

drink = raw_input("Coffee or Tea?")

def drinktype(drink):
    if drink == "Coffee":
        usercoffeetype = raw_input("What type of coffee do you drink?")
        coffee(usercoffeetype)
    elif drink == "Tea":
        print "Here is your tea."
    else:
        print "Sorry."

x = raw_input("Americano or Latte?")

def coffee(x)
    if x == "Americano":
        return "Here it is."    
    elif x == "Latte":
        return "Here is your latte."
    else:
        return "We do not have that, sorry."

Also just a note - using variable names like "x" is generally not a good idea. It'd be better if your variable name actually described what it holds, like this:

def coffee(coffeechoice)
    if coffeechoice == "Americano":
        return "Here it is."    
    elif coffeechoice == "Latte":
        return "Here is your latte."
    else:
        return "We do not have that, sorry."

Comments

0

Looks like you are trying to get something like the following

def coffee():   
    x = raw_input("Americano or Latte?")
    if x == "Americano":
        return "Here it is."    
    elif x == "Latte":
        return "Here is your latte."
    else:
        return "We do not have that, sorry."

def drinktype(drink):
    if drink == "Coffee":
        print coffee()
    elif drink == "Tea":
        print "Here is your tea."
    else:
        print "Sorry."


drink = raw_input("Coffee or Tea?")

drinktype(drink)

Please note that 1. correct indentation is crucial for you code to work 2. after defining a function drinktype(), you need to actually call it to let it run. (the last line is calling the function)

1 Comment

oh, I see now. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.