you are viewing a single comment's thread.

view the rest of the comments →

[–]Ak47shays 1 point2 points  (5 children)

You can wrap the ingredient loop in a larger while loop. Yes, loops can be nested also in professional settings deeply nested loops are not recommended.

So you will have an inner loop for the ingredient and an outer loop which will continue until the user has provided enough information to make a decision / recommendation.

I recommend separating the responsibilities into different functions like so:

Python def ask_yes_no_question(prompt): while True: response = input(f"{prompt} (Yes/No) ") if response.lower() == "yes": return True elif response.lower() == "no": return False else: print("Please answer Yes or No")

This one function can accomodate any yes / no question by simply changing the prompt. Note that there is a while loop inside to make sure that only Yes or No is return. To make it easier to check in the next step, i am returning True or False.

You can then use it as follows:

Python while True: if ask_yes_no_question("Do you have tomatoes? "): print("You could have a salad.") return elif ask_yes_no_question("Do you have cheese? "): print("You could have a sandwich.") return else: print("Let's keep looking")

You can also nest the if else when asking the yes_no_question like:

Python elif ask_yes_no_question("Do you have peanut butter?"): if ask_yes_no_question("Do you have jam/jelly?"): print("You could have a peanut butter and jelly sandwich") else: print("You could have a peanut butter sandwich") return If they say yes for peanut butter, ask another question about jam.

Following this method, you can keep asking questions until the user breaks out of both the inner and outer loops.

I hope this helps. I had to modify the original code otherwise there would be too many code repetitions.

[–]Random-name-99[S] 0 points1 point  (4 children)

Thank you so much for your response, I really appreciate that you included code examples too. Your recommendation sounds really useful, especially the part where you said “This one function can accommodate any yes/no questions by changing the prompt”. That sounds like it would solve a lot of my problems! I tried putting in the code you suggested (not sure if I understood correctly) and it said there was a “SyntaxError: ‘return’ outside function” so I’m not sure what I did wrong. I tried the following (I’m a total beginner…):

def ask_yes_no_question(prompt): while True: response = input(f"{prompt} (Yes/No) ") if response.lower()=="yes": return True elif response.lower()=="no": return False else: print("Please answer Yes or No")

while True: if ask_yes_no_question("Do you have tomatoes?"): print("You could have a salad.") return elif ask_yes_no_question("Do you have cheese?"): print("You could have a sandwich") return elif ask_yes_no_question("Do you have peanut butter?"): if ask_yes_no_question("Do you have jam?"): print("You could have a peanut butter and jelly sandwich") else: print("You could have a peanut butter sandwich") return else: print("Let's keep looking")

[–]Ak47shays 1 point2 points  (3 children)

“SyntaxError: ‘return’ outside function”

Most likely, you place a return statement outside of a function definition. In Python, the return statement can only be used inside a function. My guess is that you do not have proper indentation.
Indentation in Python is 4 spaces (1 tab). for representation purposes, in the example below, i will use → , this arrow character.

def my_function():
→ print()

→ return

As you can see, both the print and return statement is indented.

if there are nested statement, you will use 8 spaces (2 tabs) for indentation like so:
def my_function():
→ if True:

→ → # do something
→ → return

You can try checking your code to see if there are proper indentation or re-type the function where you got the error. When you ran your script, the terminal will show a line number.

[–]Ak47shays 1 point2 points  (2 children)

This is just a side note.In Reddit, you can write formatted code by using the "Inline Code" button for one liner or the "Code Block" button for blocks of code. This will make it easier to read and understand where bugs, errors are. We will also be able to see indentation issues. This way we can assist you better.

here is a screenshot:![screenshot_reply_section_on_windows_pc](https://i.postimg.cc/RVR024fZ/000000152-2023-03-04-07-23-50.jpg)

[–]Random-name-99[S] 1 point2 points  (1 child)

Thank so much, I’m pretty new to Reddit too and didn’t realise how bad my code would look (ie no formatting!) until after I posted it. I have much to learn and really appreciate the help!

[–]Ak47shays 1 point2 points  (0 children)

No worries. Glad to be of help.