104 lines
3.8 KiB
Python
104 lines
3.8 KiB
Python
import turtle
|
|
# Always put imports at the top
|
|
|
|
# We put the core logic into a function, so that in the future if we want to use one of the other functions
|
|
# (eg the get_number function), we can simply import it from this file and use it.
|
|
# If we left everything outside a function, the code would be run when trying to import one of the other functions
|
|
def run():
|
|
print('**************************************')
|
|
print('Welcome to DumbCAD 1.01')
|
|
|
|
# \n adds a new line (there is one put in automatically at the end of a print statement)
|
|
print('A Simple Programme to Draw A Regular Polygon\n')
|
|
|
|
# Move repeated logic out into another more general function so we can reuse code in the future
|
|
num_sides = get_number(minimum=3, maximum=20, prompt='Please enter number of sides (between 3 and 20):')
|
|
side_length = get_number(minimum=1, maximum=100, prompt='Please enter the length for each side:')
|
|
|
|
polygon_name = get_polyname(num_sides)
|
|
|
|
# calculate internal angle of polygon
|
|
internal_angle = (num_sides - 2) * 180 / num_sides
|
|
|
|
print(
|
|
f'Okay, a {num_sides} sided polygon is called a {polygon_name} and it has an internal angles of {internal_angle} degrees.')
|
|
print(f'Lets draw a regular {polygon_name} with {side_length} long sides.')
|
|
|
|
draw_polygon(num_sides=num_sides, side_length=side_length, internal_angle=internal_angle, polygon_name=polygon_name)
|
|
|
|
|
|
# A function that will keep asking until a number is supplied between min and max
|
|
# A default value for prompt is used if not supplied
|
|
def get_number(minimum, maximum, prompt='Please enter a number:'):
|
|
# Check whether function was supplied with sane min and max values, quit if not
|
|
if not int(minimum) or not int(maximum) or minimum > maximum:
|
|
raise ValueError('Please supply valid min and max integers')
|
|
|
|
# Loop infinitely until we reach return clause which exits function
|
|
while True:
|
|
|
|
# This block catches any errors and jumps to the 'except Exception' line below
|
|
try:
|
|
value = int(input(prompt + ' '))
|
|
|
|
# Only finish if we have a valid value
|
|
if minimum <= value <= maximum:
|
|
return value
|
|
else:
|
|
print(f"Error - number not between {minimum} and {maximum}")
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
|
|
# returns polygone name for supplied number of sides in range 3-20
|
|
def get_polyname(sides):
|
|
# dictionary of names "polynames"
|
|
polynames = {
|
|
3: "Triangle",
|
|
4: "Square",
|
|
5: "Pentagon",
|
|
6: "Hexagon",
|
|
7: "Heptagon",
|
|
8: "Octagon",
|
|
9: "Nonagon",
|
|
10: "Decagon",
|
|
11: "Hendecagon",
|
|
12: "Dodecagon",
|
|
13: "Triskaidecagon",
|
|
14: "Tetradecagon",
|
|
15: "Pendedecagon",
|
|
16: "Hexdecagon",
|
|
17: "Heptdecagon",
|
|
18: "Octdecagon",
|
|
19: "Enneadecagon",
|
|
20: "Icosagon",
|
|
}
|
|
|
|
return polynames[sides]
|
|
|
|
# Holds the logic to draw a polygon using turtle
|
|
def draw_polygon(num_sides, side_length, internal_angle, polygon_name):
|
|
turtle.screensize(4000, 4000)
|
|
turtle.hideturtle()
|
|
turtle.speed(speed=1)
|
|
turtle.bgcolor("grey")
|
|
turtle.pen(pencolor="blue", pensize=5)
|
|
|
|
for i in range(num_sides):
|
|
turtle.forward(side_length)
|
|
turtle.left(180 - internal_angle)
|
|
|
|
turtle.penup()
|
|
label_length = len(polygon_name)
|
|
turtle.goto(-label_length * 9 + label_length / 2, -60) # start position for text
|
|
turtle.write(polygon_name, font=("Verdana", 25, "normal"))
|
|
|
|
# Wont close the screen until you click
|
|
turtle.Screen().exitonclick()
|
|
|
|
|
|
# This will run the run() function only if we launched this script directly
|
|
# Eg: it will not use the run() function if this file has been loaded as an import in another script
|
|
if __name__ == '__main__':
|
|
run()
|