I have a homework assignment and I’ve done the bare minimum of the assignment, but while working on it, I became interested in how to make my program better. The point of the code is to draw a user defined shape and provide feedback if they enter invalid inputs. I want to go ahead and make it so if a user enters a str for the height/width or one of the coordinates it gives an error, but I am unable to figure out exactly how to do it. It’s an intro class so if you’d keep in mind we’re just getting to while loops and we’ve gone over for loops, if/else/etc.
Here’s the code I currently have:
def draw_user_shape(pen):
shape = input("Please enter a shape: ")
while shape != "square" and shape != "rectangle" and shape != "triangle":
shape = input("Invalid shape, please enter either square, rectangle, or triangle: ")
if shape == "square":
h = input("Please enter a height: ")
while h != int or int(h) < 1:
h = input("That is an invalid height, please enter an positive integer: ")
h = int(h)
c = input("Please enter a color: ")
while c != str and str(c) != "red" and str(c) != "green" and str(c) != "blue":
c = input("That is an invalid color, please enter either red, blue, or green: ")
c = str(c)
x = input("Please enter an x-coordinate: ")
while x != int and int(x) == 1:
x = input("That is an invalid x-coordinate, please enter an integer: ")
x = int(x)
y = input("Please enter a y-coordinate: ")
while y != int and int(y) == int:
y = input("That is an invalid y-coordinate, please enter an integer: ")
y = int(y)
pen.fillcolor(c)
pen.up()
pen.goto(x,y)
pen.down()
pen.begin_fill()
pen.goto(x,y+h)
pen.goto(x+h,y+h)
pen.goto(x+h,y)
pen.goto(x,y)
pen.end_fill()
pen.up()
elif shape == "rectangle":
h = input("Please enter a height: ")
while h != int and int(h) < 1:
h = input("That is an invalid height, please enter an positive integer: ")
h = int(h)
w = input("Please enter a width: ")
while w != int and int(w) < 1:
w = input("That is an invalid height, please enter an positive integer: ")
w = int(w)
c = input("Please enter a color: ")
while c != str and str(c) != "red" and str(c) != "green" and str(c) != "blue":
c = input("That is an invalid color, please enter either red, blue, or green: ")
c = str(c)
x = input("Please enter an x-coordinate: ")
while x != int and int(x) == 1:
x = input("That is an invalid x-coordinate, please enter an integer: ")
x = int(x)
y = input("Please enter a y-coordinate: ")
while y != int and int(y) == int:
y = input("That is an invalid y-coordinate, please enter an integer: ")
y = int(y)
pen.fillcolor(c)
pen.up()
pen.goto(x, y)
pen.down()
pen.begin_fill()
pen.goto(x,y+h)
pen.goto(x+w,y+h)
pen.goto(x+w,y)
pen.goto(x,y)
pen.end_fill()
pen.up()
elif shape == "triangle":
h = input("Please enter a height: ")
while h != int and int(h) < 1:
h = input("That is an invalid height, please enter an positive integer: ")
h = int(h)
w = input("Please enter a width: ")
while w != int and int(w) < 1:
w = input("That is an invalid height, please enter an positive integer: ")
w = int(w)
c = input("Please enter a color: ")
while c != str and str(c) != "red" and str(c) != "green" and str(c) != "blue":
c = input("That is an invalid color, please enter either red, blue, or green: ")
c = str(c)
x = input("Please enter an x-coordinate: ")
while x != int and int(x) == 1:
x = input("That is an invalid x-coordinate, please enter an integer: ")
x = int(x)
y = input("Please enter a y-coordinate: ")
while y != int and int(y) == int:
y = input("That is an invalid y-coordinate, please enter an integer: ")
y = int(y)
pen.fillcolor(c)
pen.up()
pen.goto(x,y)
pen.down()
pen.begin_fill()
pen.goto(x+w/2,y+h)
pen.goto(x+w,y)
pen.goto(x,y)
pen.end_fill()
pen.up()
def main():
import turtle
pen = turtle.Turtle()
draw_user_shape(pen)
main()
You’d want to use exception handling in this case. Basically the idea is to take the input and try to convert to an
int. If that fails, aValueErroris raised. Otherwise you will get back the converted value. Remember, the input will always be given to you as astr, so you can’t simply test if it’s anint. Assuming you are using Python 3, you can do something like this to keep asking until the right value is typed in: