【问题标题】:Why is the Except:ValueError not working in my code?为什么 except:ValueError 在我的代码中不起作用?
【发布时间】: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


【解决方案1】:

问题:为什么 except:ValueError 在我的代码中不起作用?

正如“Mike Scotty”所指出的,你是 try .. except 错误的代码行!

注意try .. except只有一行行代码!
这使您可以控制哪一行代码引发异常。

更改如下:

def farmer_johns():
    #check for valid input
    # This try ... except catches: input value != integer
    try:
        r = int(input('Enter the radius of the circle in feet:'))
    except ValueError as e:
        print('invaild input: {}'.format(e)

    ## Other code follows

这将引发错误,因为没有定义no变量input

while input >= 0 :

您可以将while True:break 结合使用。

【讨论】:

    猜你喜欢
    • 2014-01-23
    • 2013-08-06
    • 2021-03-17
    • 2018-04-24
    • 2019-01-07
    • 2014-04-28
    • 2017-06-18
    • 1970-01-01
    相关资源
    最近更新 更多