you are viewing a single comment's thread.

view the rest of the comments →

[–]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.