From f4c78985b4445e84ad04de34e0490a6f23c24c84 Mon Sep 17 00:00:00 2001 From: acid Date: Sun, 28 Nov 2021 20:28:53 +0000 Subject: [PATCH] Initial commit with refactor --- Polygon100.py | 59 ++++++++++++++++++++++++ refactored_polygon.py | 103 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100755 Polygon100.py create mode 100644 refactored_polygon.py diff --git a/Polygon100.py b/Polygon100.py new file mode 100755 index 0000000..c8c3676 --- /dev/null +++ b/Polygon100.py @@ -0,0 +1,59 @@ +print('**************************************') +print ('Welcome to DumbCAD 1.01') +print ('A Simple Programme to Draw A Regular Polygon') +print ('') + +#get "num_sides" +num_sides = int(input("How many sides is the polygon to have: between 3 and 20 ?: ")) +x = (num_sides) + +#get "length" +length = int(input("What is the length of each side ?")) +print('') + +# 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", +} +# use "num_sides" to get "figure_name" from dictionary "polynames" +figure_name = polynames[x] + +#calculate internal angle of polygon +int_angle = (num_sides-2)*180/num_sides + +print (f'Okay, a {num_sides} sided polygon is called a {figure_name} and it has an internal angles of {int_angle} degrees.') +print (f'Lets draw a regular {figure_name} with {length} long sides.') + +import turtle +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(length) + turtle.left (180 - (int_angle)) + +turtle.penup() +label=len(figure_name) +turtle.goto(-label*9+length/2,-60) #start position for text +turtle.write(figure_name, font=("Verdana",25, "normal")) +turtle.Screen().exitonclick() diff --git a/refactored_polygon.py b/refactored_polygon.py new file mode 100644 index 0000000..68f5b17 --- /dev/null +++ b/refactored_polygon.py @@ -0,0 +1,103 @@ +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()