Skip to content

Commit

Permalink
Merge pull request #92 from Chin-may02/main
Browse files Browse the repository at this point in the history
Create turtle.py
  • Loading branch information
UTSAVS26 authored Oct 4, 2024
2 parents 4210d34 + 11d6f59 commit dc81714
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions Beginner_Projects/Turtle/turtle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import turtle as t

# Setup the display window where turtle will draw
window = t.Screen()
window.setup(800, 800) # Configure Screen size
t.speed(50) # Set the speed of turtle
t.bgcolor('black') # Set background color

# Define a function to draw a spiral with a specified number of steps, a list of colors, and a direction
def spiral(steps, color_list, angle, direction):
for step in range(steps):
for c in color_list:
t.width(step / 50) # Set pen width
t.color(c) # Set turtle color
t.forward(step) # Move the turtle forward by "steps"
if direction == 'left':
t.left(angle) # Turn the turtle left
else:
t.right(angle) # Turn the turtle right

def get_user_input():
while True:
try:
total_steps = int(input("Enter the number of steps: "))
if total_steps <= 0:
raise ValueError("Number of steps must be positive.")
break
except ValueError as e:
print(f"Invalid input: {e}. Please try again.")

color_list = input("Enter the list of colors separated by commas: ").split(',')
color_list = [color.strip() for color in color_list if color.strip()] # Clean up whitespace

if not color_list:
print("You must provide at least one color.")
return None, None, None, None

while True:
try:
angle = int(input("Enter the angle for turning (suggestion: 30): "))
break
except ValueError:
print("Invalid input. Please enter a valid integer for the angle.")

while True:
direction = input("Enter the direction to turn (left/right): ").strip().lower()
if direction in ['left', 'right']:
break
else:
print("Invalid input. Please enter 'left' or 'right'.")

return total_steps, color_list, angle, direction

if __name__ == '__main__':
print("Spiral printing!!")
total_steps, color_list, angle, direction = get_user_input()

if total_steps is not None and color_list is not None:
spiral(total_steps, color_list, angle, direction)
t.done()

0 comments on commit dc81714

Please sign in to comment.