高级编程技术作业(第九、十章)

第九章

9-1 餐馆 :创建一个名为Restaurant 的类,其方法init() 设置两个属性:restaurant_name 和cuisine_type 。创建一个名为describe_restaurant() 的方法和一个名为open_restaurant() 的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。 

根据这个类创建一个名为restaurant 的实例,分别打印其两个属性,再调用前述两个方法。

class Restaurant():
	def __init__(self, restaurant_name, cuisine_type):
		self.restaurant_name = restaurant_name
		self.cuisine_type = cuisine_type
	
	def describe_restaurant(self):
		print("The name of the restaurant is "+self.restaurant_name+'.')
		print("The type of the cuisine is "+self.cuisine_type+'.')
		
	def open_restaurant(self):
		print("It's opening.")

a = Restaurant('农家乐', '小炒肉')
b = Restaurant('杨国福', '麻辣烫')
print('The first:')
print(a.restaurant_name)
print(a.cuisine_type)
a.describe_restaurant()
a.open_restaurant()

print('\nThe Second:')
print(b.restaurant_name)
print(b.cuisine_type)
b.describe_restaurant()
b.open_restaurant()

结果:

The first:
农家乐
小炒肉
The name of the restaurant is 农家乐.
The type of the cuisine is 小炒肉.
It's opening.

The Second:
杨国福
麻辣烫
The name of the restaurant is 杨国福.
The type of the cuisine is 麻辣烫.
It's opening.

9-4 就餐人数 :在为完成练习9-1而编写的程序中,添加一个名为number_served 的属性,并将其默认值设置为0。根据这个类创建一个名为restaurant 的实例;打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。 
添加一个名为set_number_served() 的方法,它让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印这个值。 

添加一个名为increment_number_served() 的方法,它让你能够将就餐人数递增。调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。

class Restaurant():
	def __init__(self, number_served=0):
		self.number_served = number_served 
	
	def set_number_served(self, num):
		print("The number of costomers served is set to " + str(num))
		self.number_served = num
	
	def increment_number_served(self, num):
		print("The number of costomers served is increased by " + str(num))
		self.number_served += num
		
a = Restaurant()
print("The defalt number of costomers served is " + str(a.number_served))
print('')
a.number_served = 20
print("The number of costomers served is now " + str(a.number_served))
print('')
a.set_number_served(30)
print("The number of costomers served is now " + str(a.number_served))
print('')
a.increment_number_served(50)
print("The number of costomers served is now " + str(a.number_served))

结果:

The defalt number of costomers served is 0

The number of costomers served is now 20

The number of costomers served is set to 30
The number of costomers served is now 30

The number of costomers served is increased by 50
The number of costomers served is now 80

9-6 冰淇淋小店 :冰淇淋小店是一种特殊的餐馆。编写一个名为IceCreamStand 的类,让它继承你为完成练习9-1或练习9-4而编写的Restaurant 类。这两个版本的Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋的方法。创建一个IceCreamStand 实例,并调用这个方法。

class Restaurant():
	def __init__(self, number_served=0):
		self.number_served = number_served 
	
	def set_number_served(self, num):
		print("The number of costomers served is set to " + str(num))
		self.number_served = num
	
	def increment_number_served(self, num):
		print("The number of costomers served is increased by " + str(num))
		self.number_served += num
		
class IceCreamStand(Restaurant):
	def __init__(self, number_served, flavors):
		super().__init__(number_served)
		self.flavors = flavors
	
	def show_flavors(self):
		for flavor in self.flavors:
			print(flavor.title())

a = IceCreamStand(20, ['strawberry', 'vanilla'])
a.show_flavors()

结果:

Strawberry
Vanilla


第十章

10-1 Python学习笔记 :在文本编辑器中新建一个文件,写几句话来总结一下你至此学到的Python知识,其中每一行都以“In Python you can”打头。将这个文件命名为 learning_python.txt,并将其存储到为完成本章练习而编写的程序所在的目录中。编写一个程序,它读取这个文件,并将你所写的内容打印三次:第一次打印时读取整个文件;第二次打印时遍历文件对象;第三次打印时将各行存储在一个列表中,再在with 代码块外打印它们

with open('learning_python.txt') as f:
	print(f.read())
	
with open('learning_python.txt') as f:
	for line in f:
		print(line.rstrip())
	print('\n')
	
with open('learning_python.txt') as f:
	content = f.readlines()
for line in content:
	print(line.rstrip())

结果:

In Python you can create game.
In Python you can bulid your web.
In Python you can process natrual language.

In Python you can create game.
In Python you can bulid your web.
In Python you can process natrual language.

In Python you can create game.
In Python you can bulid your web.
In Python you can process natrual language.


10-6 加法运算 :提示用户提供数值输入时,常出现的一个问题是,用户提供的是文本而不是数字。在这种情况下,当你尝试将输入转换为整数时,将引发ValueError 异常。编写一个程序,提示用户输入两个数字,再将它们相加并打印结果。在用户输入的任何一个值不是数字时都捕获ValueError 异常,并打印一条友好的错误消息。对你编写的程序进行测试:先输入两个数字,再输入一些文本而不是数字。

def test():
	print('Please input two integers.')
	flag = True
	while flag:
		try:
			a = int(input('The first is:'))
			b = int(input('The second is:'))
		except ValueError:
			print('Invalid input! Please input again.\n')
		else:
			print('The sum is ' + str(a+b))
			flag = False
		
test()

结果:

Please input two integers.
The first is:a
Invalid input! Please input again.

The first is:12
The second is:e
Invalid input! Please input again.

The first is:12
The second is:3
The sum is 15

10-11 喜欢的数字 :编写一个程序,提示用户输入他喜欢的数字,并使用json.dump() 将这个数字存储到文件中。再编写一个程序,从文件中读取这个值,并打印消息“I knowyour favorite number! It’s _.”。

程序1:

import json

favorite_num = input('Please input one of your favorite numbers:')
with open('a.json','w') as f:
    json.dump(favorite_num, f)

结果1:

Please input one of your favorite numbers:2

程序2:

import json

with open('a.json') as f:
	favorite_num = json.load(f)
	print('Your favorite number is ' + favorite_num)
  
结果2:
Your favorite number is 2

Please input one of your favorite numbers:2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值