Python exercise of class,error handling

Today I try to write a English blog to review the knowledge which learning from the class

#exercise 1
class Person:
    def __init__(self, fname, lname):
        self.firstname = fname        
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

class Student(Person):
    name=''
    def printname(self):
        return str(self.name)

class Teacher(Person):
    def __init__(self, fname, lname,department):
        super().__init__(fname,lname)
        self.department = department

    def printname(self):
        print(self.firstname, self.lastname,self.department)


x = Student("Bob", "Bobba")
x.printname()
y = Person("Bob", "Bobba")
y.printname()
z = Teacher("GEOGE","KENYA","wow")
print(z.department)
z.printname()

#exercise 2
#use the try,except,else,finally to raise the exceptions
try:
    x = 1 / 0        #error
    print(x)
except Exception:
    print("An error occured")

try:
    princesses = [
        {"name": "Ariel", "age": 16, "prince": "Eric"},
        {"name": "Jasmine", "age": 16, "prince": "Aladdin"},
        {"name": "Rapunzel", "age": 18, "prince": "Flynn Rider"},
        {"name": "Belle", "age": 17, "beast": "The Beast"},
        {"name": "Mulan", "age": 16, "prince": "Li Shang"}
    ]
except Exception as result:
    for princess in princesses:
        print("Princess {princess['name']} is {princess['age']} years old.")
    print("Her prince is {princess['prince']}")
else:
    pass
finally:
    print("These are the all of the royal's families")

#exercise 3
#I want to know how the keys and words to order
#If there haven't any word in the dictionary,how do the keys ordered 
a={"18","4","12"}
def _print_people(b,c,d):
    print(b)
    print(c)
    print(d)
print(a)
print(*a)
_print_people(20,40,60)
_print_people(*a)

conclusion of my exercise 3 that If there only have keys in the dictionary, they are disordered,

thanks for reading

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值