0

I have been developing a fairly simple turtle program which essentially just draws snowflakes of a random size, shape, and colour. The amount is based on user input. What I would like to do with the program is add an input section at the end that asks the user whether they would like to end the program or restart it. If they choose restart, the program should start over again from the beginning. Otherwise it should follow the exit procedure I made for it. I have seen methods including WHILE Loops and other similar concepts, I just can't figure out how to apply it to my code. Is there a simple way to do this?

Here is the code:

import time
import sys
import turtle
import random
n = int(input("How many snowflakes do you want?: "))
screen = turtle.Screen()
screen.bgcolor("black")

speedy = turtle.Turtle()
speedy.speed(0)

sfcolor = ["yellow","gold","orange","red","violet","magenta","purple","navy","blue","skyblue","cyan","turquoise","lightgreen","green","darkgreen","white"]

def snowflake(size):
  speedy.penup()
  speedy.forward(10 * size)
  speedy.left(45)
  speedy.pendown()
  speedy.color(random.choice(sfcolor))
  for i in range(8):
    branch(size)  
    speedy.left(45)
   
def branch(size):
  for i in range(3):
    for i in range(3):
      speedy.forward(10.0 * size / 3)
      speedy.back(10.0 * size / 3)
      speedy.right(45)
    speedy.left(90)
    speedy.back(10.0 * size / 3)
    speedy.left(45)
  speedy.right(90)
  speedy.forward(10.0 * size)


for i in range(n):
  x = random.randint(-200, 200)
  y = random.randint(-200, 200)
  sfsize = random.randint(1, 4)
  speedy.penup()
  speedy.goto(x, y)
  speedy.pendown()
  snowflake(sfsize)

print("The turtle window will close in 10 seconds. Thanks for using the program!")
print("Goodbye!")
time.sleep(10)
turtle.Screen().bye()
sys.exit()
Josh
  • 3
  • 5
  • Yup, just toss a `while True:` around your code. – ggorlen Oct 03 '22 at 16:53
  • 1
    Does this answer your question? [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) – ggorlen Oct 03 '22 at 16:54
  • Problem is, I'm not sure how to implement the while loop, because it messes up all the indentation, how would I fix this? – Josh Oct 11 '22 at 10:07
  • Indent all of the code you want to be in the loop by one level. Or write a function and call it in a loop. If you're stuck, try simplifying the problem. Just make a text-only app that prompts until valid, or re-runs a simple `print(user_response)` to substitute for the game. Then move that to a function. Then once all of that is working, drop in your actual turtle code. – ggorlen Oct 11 '22 at 15:11

1 Answers1

0

Solved! Thanks to the advice from the user https://stackoverflow.com/users/6243352/ggorlen

This is my complete code now that it works.

import time
import sys
import turtle
import random
restart = True
while restart == True:
 n = int(input("How many snowflakes do you want?: "))
 screen = turtle.Screen()
 screen.bgcolor("black")

 speedy = turtle.Turtle()
 speedy.speed(0)

 sfcolor = ["yellow","gold","orange","red","violet","magenta","purple","navy","blue","skyblue","cyan","turquoise","lightgreen","green","darkgreen","white"]

 def snowflake(size):
   speedy.penup()
   speedy.forward(10 * size)
   speedy.left(45)
   speedy.pendown()
   speedy.color(random.choice(sfcolor))
   for i in range(8):
     branch(size)  
     speedy.left(45)
   
 def branch(size):
   for i in range(3):
     for i in range(3):
       speedy.forward(10.0 * size / 3)
       speedy.back(10.0 * size / 3)
       speedy.right(45)
     speedy.left(90)
     speedy.back(10.0 * size / 3)
     speedy.left(45)
   speedy.right(90)
   speedy.forward(10.0 * size)


 for i in range(n):
   x = random.randint(-200, 200)
   y = random.randint(-200, 200)
   sfsize = random.randint(1, 4)
   speedy.penup()
   speedy.goto(x, y)
   speedy.pendown()
   snowflake(sfsize)
 print("The turtle window will close in 10 seconds. Thanks for using the program!")
 time.sleep(10)
 turtle.Screen().bye()
 restart = False
 restart = input("Do you want to restart the program? Yes or No: ")
 restart = restart.upper()
 if restart == "YES":
   restart = True
   print("Restarting...")
 elif restart == "NO":
   restart = False
   print("Thank you for using the program. Goodbye!")
   time.sleep(10)
   turtle.Screen().bye()
   sys.exit()
 else:
   print("Error.")
Josh
  • 3
  • 5