-2

In my program, a user selects to have a value chosen at random. If they want to choose again at random how do I loop back a few lines to run that code again?

For context, it's a recipe program. If they don't like the recipe randomly chosen, they can pick at random again.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Welcome to StackOverflow. Please see https://stackoverflow.com/help/how-to-ask and provide the Python program you'd like us to look at. – rajah9 Jul 14 '21 at 12:22
  • 1
    The answer is in the title... Use a [loop](https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming) – Tomerikoo Jul 14 '21 at 12:24
  • 1
    Does this answer your question? [How to make program go back to the top of the code instead of closing](https://stackoverflow.com/questions/18791882/how-to-make-program-go-back-to-the-top-of-the-code-instead-of-closing) – Tomerikoo Jul 14 '21 at 12:28

3 Answers3

1

Loop may be what you want. P.S. It would be helpful to study programming by reading the textbook from the beginning to the end.

user26742873
  • 919
  • 6
  • 21
1
user_unhappy = True
while user_unhappy:
    #do stuff
    #do stuff
    user_input = input("ask question")
    #do stuff
    ask_user_happy = input("happy with choice?")
    if ask_user_happy.casefold() == "y" or ask_user_happy.casefold() == "ye" or ask_user_happy.casefold() == "yes":
        user_unhappy = False
Panda
  • 91
  • 1
  • 12
1

I think the solution to this would be to nest one loop within the other. The outer infinite loop for selecting the recipe and the inner loop for going through the lines of the recipe. If the user wants to choose a new recipe, you can break out of the inner loop and show the recipes list again.

while True:
    # Show and select recipe.
    for line in recipe_lines:
        # Show recipe.
        # Break if recipe change
    # Break from outer loop if no new recipe is chosen.
NiceOne
  • 36
  • 4