《Python编程-从入门到实践》课后习题(11)

原创 2018年04月15日 22:52:25

11-1

city_functions.py

def foo(city, country):
    return city.title()+', '+country.title()

test_cities.py

import unittest
from city_functions import foo

class test(unittest.TestCase):
    def test_city_country(self):
        cityname = foo('santiago', 'chile')
        self.assertEqual(cityname, 'Santiago, Chile')

unittest.main()

11-2

修改前

city_functions.py

def foo(city, country, population=0):
    return city.title()+', '+country.title()+' - population '+str(population)

test_cities.py

import unittest
from city_functions import foo

class test(unittest.TestCase):
    def test_city_country(self):
        cityname = foo('santiago', 'chile')
        self.assertEqual(cityname, 'Santiago, Chile')

unittest.main()

修改后

city_functions.py

def foo(city, country, population=0):
    return city.title()+', '+country.title()+' - population '+str(population)

test_cities.py

import unittest
from city_functions import foo

class test(unittest.TestCase):
    def test_city_country(self):
        cityname = foo('santiago', 'chile')
        self.assertEqual(cityname, 'Santiago, Chile')
    def test_city_country_population(self):
        cityname = foo('santiago', 'chile', population=5000000)
        self.assertEqual(cityname, 'Santiago, Chile - population 5000000')

unittest.main()

11-3

import unittest

class Employee():
    def __init__(self, first_name, last_name, salary):
        self.first_name = first_name
        self.last_name = last_name
        self.salary = salary
    def give_raise(self, dollar = 5000):
        self.salary += dollar

class TestEmployee(unittest.TestCase):
    def setUp(self):
        self.employee = Employee(0, 0, 0)
        default_salary = 5000
        custom_salary = 10000
    def test_give_default_raise(self):
        self.employee.give_raise()
        self.assertEqual(5000, self.employee.salary)
    def test_give_default_raise(self):
        self.employee.give_raise(dollar=1000)
        self.assertEqual(1000, self.employee.salary)

unittest.main()

Python编程:从入门到实践(课后习题4)

# 4-1 比萨 pizzas = ['seafood', 'sausage', 'cheese'] for i in pizzas: print(i) for i in pizzas: ...
  • z120379372
  • z120379372
  • 2017-09-19 11:01:33
  • 2310

Python编程:从入门到实践(课后习题10)

# 10-6 加法运算 first_number = input("First number: ") second_number = input("Second number: ") try: ad...
  • z120379372
  • z120379372
  • 2017-09-19 11:08:31
  • 1349

Python编程:从入门到实践(课后习题6)

# 6-1 人 name = { 'first_name': 'lili', 'last_name': 'zhang', 'age': 18, 'city': 'guangzhou' }...
  • z120379372
  • z120379372
  • 2017-09-19 11:03:26
  • 1773

Python编程从入门到实践2-11章课后答案

  • 2018年03月24日 20:26
  • 84KB
  • 下载

python编程:从入门到实践及课后答案

  • 2018年02月05日 00:06
  • 6.52MB
  • 下载

《python从入门到实践》课后试一试代码

  • 2017年10月12日 10:15
  • 456KB
  • 下载

Python编程:从入门到实践(课后习题5)

# 5-3 外星人颜色#1 alien_color = 'green' if alien_color == 'green': print('You get 5 point.') alien_colo...
  • z120379372
  • z120379372
  • 2017-09-19 11:02:12
  • 2016

Python编程从入门到实践课后题答案

  • 2018年02月20日 18:35
  • 8.16MB
  • 下载

Python编程:从入门到实践(课后习题8)

# 8-1 消息 def display_message(): print("This chapter I will learning function.") display_message() ...
  • z120379372
  • z120379372
  • 2017-09-19 11:05:38
  • 2840

Python编程:从入门到实践(课后习题7)

# 7-1 汽车租 car = input("What kind of car do you want to rent: ") print("Let me see if I can find you ...
  • z120379372
  • z120379372
  • 2017-09-19 11:04:07
  • 1861
收藏助手
不良信息举报
您举报文章:《Python编程-从入门到实践》课后习题(11)
举报原因:
原因补充:

(最多只允许输入30个字)