【发布时间】:2019-03-12 02:27:33
【问题描述】:
这是我的源代码:
def farmer_johns():
r = int(input('Enter the radius of the circle in feet:'))
#check for valid input
try:
while input >= 0 :
# area of brown
#area of square - area of circle
import math
pi = math.pi
area_square = (r+r)**2
area_circle = pi* r**2
area_brown = area_square - area_circle
print('The area of the brown shaded region is {:.2f} feet^2.'.format(area_brown))
#turtle graphics set up
import turtle
wn = turtle.Screen()
a = turtle.Turtle()
a.pensize(5)
#draw circles
a.pencolor('green')
a.penup()
a.goto(r,r)
a.pendown()
a.circle(r)
a.penup()
a.goto(r,-r)
a.pendown()
a.circle(r)
a.penup()
a.goto(-r,-r)
a.pendown()
a.circle(r)
a.penup()
a.goto(-r,r)
a.pendown()
a.circle(r)
#square
a.pencolor('blue')
a.penup()
a.goto(r,r+r)
a.pendown()
a.goto(-r,r+r)
a.goto(-r,-r+r)
a.goto(r,-r+r)
a.goto(r,r+r)
#middle region
a.pencolor('#654321')
a.fillcolor('#b5651d')
a.begin_fill()
a.penup()
a.goto(r,r)
a.pendown()
a.circle(r,-90)
a.penup()
a.goto(0,r+r)
a.right(180)
a.pendown()
a.circle(r,-90)
a.penup()
a.goto(-r,r)
a.left(-180)
a.pendown()
a.circle(r,-90)
a.penup()
a.goto(0,0)
a.left(180)
a.pendown()
a.circle(r,-90)
a.end_fill()
#writing
# a.write('The area of the brown shaded region is {:.2f} feet^2.'.format(area_brown))
except ValueError:
print('invaild input')
except TypeError:
print('invaild input')
问题是当我输入字母作为输入时,我得到了这个:
farmer_johns() 以英尺为单位输入圆的半径:Ee 回溯(最近一次通话最后): 文件“”,第 1 行,在 文件“C:\Users\SueAnn\Desktop\Comp Sci\Projects\project_3\farmer john again.py”,第 19 行,在 farmer_johns r = int(input('以英尺为单位输入圆的半径:')) ValueError: int() 以 10 为底的无效文字:'Ee'
“Except:TypeError”行有效,但“Except:ValueError”行
【问题讨论】:
-
例外在您的
try...except块之外。 -
你对
int('Ee')有什么期望? -
我希望它打印“无效输入”。
-
另外,如果我缩进显示“除 ValueError”的行,使其位于 try...except 块内,则代码甚至不会运行。它说这是“无效的语法”。
标签: python typeerror valueerror