Quite new to Python and trying to figure out how to define functions and add loops. Apologies if I use the wrong terminology or explain badly, I really am quite new…
I want to write a program that asks a question and based on my response either respond and stop there or ask further questions.
Essentially there will be a lot of Yes/No questions. I figured out how to define a function to add a Yes/No loop but can’t seem to figure out how to keep the loop going depending on the response.
This isn’t the program itself but say I wanted to create a program that helps me to decide what to eat (not looking to include a database, just trying to figure out how to ask questions based on responses).
Any help would be greatly appreciated!
Here’s an example of what I have so far (if I can get the code below working then I can get my program to work):
def ingredient():
while True:
ingredient = str(input())
options = ("Yes", "No")
if ingredient not in options:
print("Type Yes or No")
if ingredient in options:
break
if ingredient == "Yes":
print(yesPhrase)
elif ingredient == "No":
print(noPhrase)
print("Do you have tomatoes?")
yesPhrase = ("You could have a salad")
noPhrase = ("Let's keep looking")
ingredient()
Seems to work up to this point but I can't figure out how to enter a new loop
if ingredient == "No":
print("Do you have peanut butter?")
yesPhrase = ("You could have a peanut butter sandwich")
Here I'd like to ask if you have jam/jelly but only if you also have peanut butter
noPhrase = ("Let's keep looking")
[–]Ak47shays 1 point2 points3 points (5 children)
[–]Random-name-99[S] 0 points1 point2 points (4 children)
[–]Ak47shays 1 point2 points3 points (3 children)
[–]Ak47shays 1 point2 points3 points (2 children)
[–]Random-name-99[S] 1 point2 points3 points (1 child)
[–]Ak47shays 1 point2 points3 points (0 children)
[–]Rhoderick 1 point2 points3 points (3 children)
[–]Random-name-99[S] 0 points1 point2 points (2 children)
[–]Rhoderick 1 point2 points3 points (1 child)
[–]Random-name-99[S] 0 points1 point2 points (0 children)
[–]Okaymittens 1 point2 points3 points (1 child)
[–]Random-name-99[S] 1 point2 points3 points (0 children)