Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function and Input
#1
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()
Reply
#2
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))
   
Reply
#3
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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between forms of input a list to function akbarza 6 1,021 Feb-21-2024, 08:02 PM
Last Post: bterwijn
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,076 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Showing an empty chart, then input data via function kgall89 0 975 Jun-02-2022, 01:53 AM
Last Post: kgall89
  input function question barryjo 12 2,705 Jan-18-2022, 12:11 AM
Last Post: barryjo
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,108 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  Problem with input after function luilong 10 4,075 Dec-04-2021, 12:16 AM
Last Post: luilong
  Exit function from nested function based on user input Turtle 5 2,896 Oct-10-2021, 12:55 AM
Last Post: Turtle
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 1,960 Oct-06-2021, 09:39 AM
Last Post: menator01
  Input function cutting off commands at spaces. throwaway34 3 2,194 May-12-2021, 06:40 AM
Last Post: throwaway34
  Defining a function with input abcd 5 3,103 Feb-21-2021, 02:34 AM
Last Post: NullAdmin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020