美文网首页Python三期爬虫作业
【笨办法学Python】- 习题39、40、45

【笨办法学Python】- 习题39、40、45

作者: 927556dd6e46 | 来源:发表于2017-07-31 18:06 被阅读24次

习题 39

#   2017-07-24
ten_things = "Apples Oranges Croows Telephone Light Suger"
print("What there's not 10 thigs in that list,let's fix that.")
stuff = ten_things.split(' ')
more_stuff = ["day","night","song","frisbbe","corn","banana","girl","boy"]
while len(stuff) != 10:
        next_one = more_stuff.pop()
        print("Adding: ",next_one)
        stuff.append(next_one)
        print("There's %d items now." % len(stuff))
print("There we go: ",stuff)
print("Let's do some things with stuff.")
print(stuff[1])
print(stuff[-1])
print(stuff.pop())
print(' '.join(stuff))
print('#'.join(stuff[3:5]))

习题39

习题 40

#   习题 40:字典,可爱的字典
cities = {'CA':'San Francisco','MI':'Detroit','FL':'Jacksonville'}
cities['NY']='New York'
cities['OR']='Portland'

def find_city(thempt,state):
    if state in thempt:
        return thempt[state]
    else:
        return "Not found"

cities['_find']=find_city
while True:
    print("State? (ENTER to quit)"),
    state=input(">")
    if not state:break
    city_found=cities['_find'](cities,state)
    print(city_found)

运行:

"C:\Program Files\Python36\python.exe" D:/python3_project/practice/week3/ex40.py
State? (ENTER to quit)
>CA
San Francisco
State? (ENTER to quit)
>FL
Jacksonville
State? (ENTER to quit)
>o
Not found
State? (ENTER to quit)
>OR
Portland
State? (ENTER to quit)
>VT
Not found
State? (ENTER to quit)
>

习题 45

class Animal(object):
    pass

class Dog(Animal):
    def __init__(self, name):
        self.name = name

class Cat(Animal):
    def __init__(self, name):
        self.name = name

class Person(object):
    def __init__(self, name):
        self.name = name
        self.pet = None

class Employee(Person):
    def __init__(self, name, salary):
        super(Employee, self).__init__(name)
        self.salary = salary

class Fish(object):
    pass

class Salmon(Fish):
    pass

class Halibut(Fish):
    pass

rover = Dog("Rover")

satan = Cat("Satan")

mary = Person("Mary")

mary.pet = satan

frank = Employee("Frank", 120000)

frank.pet = rover

flipper = Fish()

crouse = Salmon()

harry = Halibut()

python中self和__init__的含义 以及self和__init__的应用场景确实还存有疑问

相关文章

网友评论

    本文标题:【笨办法学Python】- 习题39、40、45

    本文链接:https://www.haomeiwen.com/subject/tctikxtx.html