我是一名高中生,在过去的几个月里一直和python一起工作。我完成了这个程序,它将方块绘制到屏幕上,用户可以输入方块的大小、颜色和数量。我问我的老师如何进一步改进这个程序,他提到了错误和例外。我将其部分实现到代码中,但似乎不正确。他建议在这个网站上寻找答案和可能的帮助。我感谢你的任何建议和指导。
import turtle
window = turtle.Screen()
elon = turtle.Turtle()
varX = -175
varY = 200
elon.penup()
userPenSize = input("Hey what size pen do you want?" "(0-10) ")
#^ asking user the size of pen they would like
userColor = input("What color do you want? " "(blue, yellow, orange, pink) ")
#^ asking user the color pen they want to use
hmTri = input("How many triangles do you want? ")
#^ asking user how many squares they want drawn to screen
for bigCount in range(int(hmTri)): #input from user of how many squares will be drawn
Exception (ValueError)
print ("Could not convert data to an integer.")
elon.goto(varX, varY)
elon.pendown()
for smallCount in range(4):
elon.pensize(int(userPenSize)) #input from user for pen size
Exception (ValueError)
print ("Could not convert data to an integer.")
elon.pencolor(str(userColor)) #input from user for pen color
elon.forward(100)
elon.right(90)
varX = varX + 50 #moving 50 pixels on x axis each square
varY = varY - 50 #moving -50 pixels on y axis each square
elon.penup()发布于 2020-05-20 16:54:29
用“尝试.除了.”就像下面,
try:
hmTri = input("How many triangles do you want? ")
num = int(hmTri)
except ValueError:
# maybe input "Hello World"
print('Only input numbers')
except:
# other wrong format
print('Something Wrong')https://stackoverflow.com/questions/61918375
复制相似问题