-1
def life():
    print("You wake up")
    eat = input("are you hungry? Please Enter y or n.")
    if eat.upper() == "Y":
        print("I predict you will eat breakfast")
    elif eat.upper() == "N":
        print("I predict you will skip breakfast")
    while eat.upper() not in ("Y","N"):
        print("input not accepted")
        eat = input("are you hungry? Please Enter y or n.")
        if eat.upper() == "Y":
            print("I predict you will eat breakfast")
        elif eat.upper() == "N":
            print("I predict you will skip breakfast")
    print("You leave the house")
    day = input("is it cloudy? please enter y or n")
    if day.upper() == "Y":
        print("I predict you will bring an umbrella")
    elif day.upper() == "N":
        print("I predict you will wear sunglasses")
    while day.upper() not in ("Y","N"):
        print("input not accepted")
        day = input("is it cloudy? please enter y or n")
        if day.upper() == "Y":
            print("I predict you will bring an umbrella")
        elif day.upper() == "N":
            print("I predict you will wear sunglasses")
    print("You go out for dinner")
    food = input("Do you want meat? please enter y or n")
    if food.upper() == "Y":
        print("I predict you will order steak!")
    elif food.upper() == "N":
        pass
    while food.upper() not in ("Y", "N"):
        print("input not accepted")
        food = input("Do you want meat? please enter y or n")
        if food.upper() == "Y":
            print("I predict you will order steak!")
        elif food.upper() == "N":
            pass
    pasta = input("Do you want Pasta: please enter y or n")
    if pasta.upper() == "Y":
        print("I predict you will order spaghetti and meatballs!!")
    elif pasta.upper() == "N":
        print("I predict you will order salad.")
    while pasta.upper() not in ("Y", "N"):
        print("input not accepted")
        pasta = input("Do you want Pasta: please enter y or n")
        if pasta.upper() == "Y":
            print("I predict you will order spaghetti and meatballs!!")
        elif pasta.upper() == "N":
            print("I predict you will order salad.")
life()

I feel like it is unnecessary to repeat multiple lines of code in the while statement. Is there any way to make the while statement go to a previous line of code and run it from there instead of re-entering the same code back into the while statement. The code seems to run fine but I would like it to be less clunky if possible. Thanks!

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 5
    [codereview.se] – Sayse Feb 17 '20 at 23:02
  • 2
    A Stack Overflow question should be about a *specific* problem, with only the *shortest* code that demonstrates it included. As the terser comment above says, questions asking for general suggestions on how to improve *working* code belong on our sister site [codereview.se] instead (subject to its rules). See [A Guide To Code Review For Stack Overflow Users](https://codereview.meta.stackexchange.com/questions/5777/a-guide-to-code-review-for-stack-overflow-users). – Charles Duffy Feb 17 '20 at 23:03
  • 2
    Create a function for taking input from the user and use it instead of all the while loops. Make the function take a parameter that is the prompt. See [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) to help you – Tomerikoo Feb 17 '20 at 23:04
  • I had some answer for you but unfortunately the 'trigger happy' people closed this question before I had a chance to post it. Here: https://www.paste.org/103044 – tonypdmtr Feb 17 '20 at 23:43

1 Answers1

0

There are several modifications I would suggest.

  1. Use inline conditional statements <expression1> if <condition> else <expression2>

  2. Use string formatting "Today is {}".format(date)

So you can write

s = "eat" if eat.upper() == "Y" else "skip" if eat.upper() == "N" else ""
print("I predict you will {} breakfast".format(s))

Be sure to check that eat.upper() is "Y" or "N" first, or the code would fail.

Since your code for eat and day are similar, you could also use a function to simplify your code.

Johnson Zhou
  • 443
  • 3
  • 13
  • 3
    Much better now. That said, do note the "Answer Well-Asked Questions" section of [How to Answer](https://stackoverflow.com/help/how-to-answer), particularly the bullet point around questions that "require too much guidance for you to answer in full, or request answers to multiple questions". By nature, open-ended questions (of which "how can this code be improved?" is an example) cannot be answered *in full*. – Charles Duffy Feb 17 '20 at 23:15
  • That said, I'm not sure this code is actually valid Python. See https://ideone.com/oS91g7 throwing a SyntaxError -- is there a reason you're separately testing for the `N` case, instead of just falling through to `'skip'` if the `Y` test failed? – Charles Duffy Feb 17 '20 at 23:28
  • I have edited the answer, should work fine now. – Johnson Zhou Feb 17 '20 at 23:47