Python: Modify this program of a spiral square so that it draws a hexagon (six-s
ID: 3681805 • Letter: P
Question
Python:
Modify this program of a spiral square so that it draws a hexagon (six-sided polygon) as in the picture
from turtle import *
import turtle
screen=Screen()
#getting width and height
width = screen.screensize()[0]
height= screen.screensize()[1]
#reading inputs
initialLength = int(input("length of initial line: "))
decr = int(input("Amount of decrement between each lines to draw: "))
#checking inputs for width and height
if(initialLength < width and initialLength<height):
turn = 90
start = height-3;
Draw = turtle.Turtle()
# filling sppiral square lines
while start < height:
Draw.forward(start) #drawing lines
Draw.right (turn)
start -= decr #decrementing every time
done()
else:
#if inputs are wrong
print('Inputs greater than screen width and height')
The program should still promot the user for the following inputs:
- The length of the initial (or first) line to draw
- The amount of decrement between each two lines to draw
Explanation / Answer
If everything else works fine, the only thing you're supposed to do is change the angle from 90 degrees to 60 degrees, which will change the polygon from square to hexagon. Here is the modified code for you:
#! /usr/bin/python
from turtle import *
import turtle
screen=Screen()
#getting width and height
width = screen.screensize()[0]
height= screen.screensize()[1]
#reading inputs
initialLength = int(input("length of initial line: "))
decr = int(input("Amount of decrement between each lines to draw: "))
#checking inputs for width and height
if(initialLength < width and initialLength<height):
turn = 60
start = 3;
Draw = turtle.Turtle()
# filling sppiral square lines
while start < height:
Draw.forward(start) #drawing lines
Draw.right (turn)
start -= decr #decrementing every time
done()
else:
#if inputs are wrong
print('Inputs greater than screen width and height')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.