У меня есть домашнее задание, и я выполнил минимум задания, но, работая над ним, я заинтересовался, как улучшить свою программу. Смысл кода состоит в том, чтобы нарисовать определяемую пользователем форму и предоставить обратную связь, если они вводят неверные данные. Я хочу пойти дальше и сделать так, чтобы пользователь вводил str для высоты / ширины или одну из координат, это выдает ошибку, но я не могу понять, как именно это сделать. Это вводный класс, так что, если вы помните, мы просто переходим к циклам while и переходим к циклам, если / else / etc.
Вот код, который у меня сейчас есть:
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()