Python异常无法正常工作,ValueError(异常存储桶)

时间:2015-08-09 23:27:42

标签: python exception-handling

import math, random

try:  
    class Circle:
        def __init__(self, radius):
            if radius == 0:
                self.radius = random.uniform(1.1,2.5)
            else:
                self.radius = radius         
        def clc(self):
            return math.pi * 2 * self.radius

        def dia(self):
            return self.radius * 2 

        def area(self):
            return math.pi * (self.radius ** 2)

        def dia2(self):
            return self.radius * 4


    circles = []

    for i in list(range(0,2)):
        s = int(input("enter te radius of the circle : "))
        c = Circle(s)
        circles.append(c)

except (ValueError) as e:
    print('There was an error, try again ?') 
    print('You keyed a char insted of a number:',e) 
else: 
    print("radius:", c.radius,
          "cirm:", c.clc(),'Diameter:',
          c.dia(), 'area:', c.area(),
          'dia2:', c.dia2())

0 个答案:

没有答案