Python Forum

Full Version: Function and Input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone, I need some guidance on some basic stuff. Been trying to find an answer but it's been hard to find.

This is what I'm trying to do, I'm trying to use if statements to go from one function to another. I'm use to using "goto" statements but
I know those just make messy code and aren't really used from what I read. Below is an example

I want to make it so if they don't answer y or n that they will be returned to the beginning but if they answer y or n that they will be moved
to the next function with the next set of instructions. How would I do this?

thank you again everyone :)

def user():
    command = input("Please enter y for cookies, n for cupcakes > ")
    if command == "y":
        print("Here are your cookies")
    elif command == "n":
        print("Here are your cupcakes")
    else:
        print("Please enter y or n for your selection")
        return cookies()


def cookies():
    print("testing")


user()
cookies()
I didn't understand much but it seems "while" cycle is what you need.
command = input("Enter y or n")
while command not in ("y", "n"):
    print("I said y or n")
    command = input("Enter y or n")
print("OK, your command is {}".format(command))
   
That will definitely help with the loop, pretty much this is how I'd like it to go..

If they answer "y" or "n" then I want them to be directed to the next function.. which would be cookies().
but the while will help if they don't answer "y" or "n" then that will send them to the beginning of the first function which is user() until they give an answer.
return cookies()
With the presented while-loop you donĀ“t need this, as it is also not good coding.
You are calling the function cookies, ok atm there is only a print in it and as there is no return statement in cookies this function returns the default value None.
And this value None is returned to your program root.
But in your root you are already calling cookies() after user() is finished.