defdescribe_battery(self): """ 打印一条描述电瓶容量的消息 """ print("This car has a " + str(self.battery_size) + "-kWh battery.")
defget_range(self): """ 打印一条消息,指出电瓶的续航里程 """ if self.battery_size == 70: range = 240 elif self.battery_size == 85: range = 270 message = "This car can go approximately " + str(range) message += " miles on a full charge." print(message)
defupgrade_battery(self): if self.battery_size != 85: self.battery_size = 85
classCar: """ 一次模拟汽车的简单尝试 """
def__init__(self, make, model, year): self.make = make self.model = model self.year = year self.odometer_reading = 0
defread_odometer(self): print("This car has " + str(self.odometer_reading) + " miles on it.")
defupdate_odometer(self, mileage): if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print("You can't roll back an odometer!")
defincrement_odometer(self, miles): self.odometer_reading += miles
from user import User from admin import Privileges, Admin a = Admin('a', 'b') a.show_privileges()
##9-15 Python Module of the Week :要了解 Python 标准库,一个很不错的资源是网站 Python Module of the Week 。请访问 http://pymotw.com/ 并查看其中的目录,在其中找一个你感兴趣的模块进行探索,或阅读模块 collections 和 random 的文档。
1
https://pymotw.com/3/re/index.html
##10-1 Python 学习笔记 :在文本编辑器中新建一个文件,写几句话来总结一下你至此学到的 Python 知识,其中每一行都以 “In Python you can” 打头。将这个文件命名为learning_python.txt ,并将其存储到为完成本章练习而编写的程序所在的目录中。编写一个程序,它读取这个文件,并将你所写的内容打印三次:第一次打印时读取整个文件;第二次打印时遍历文件对象;第三次打印时将各行存储在一个列表中,再在 with 代码块外打印它们。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
file = 'learning_python.txt' with open(file, 'r', encoding='UTF-8') as f: m = f.read() print(m)
with open(file, 'r', encoding='UTF-8') as f: for line in f: print(line)
with open(file, 'r', encoding='UTF-8') as f: ml = f.readlines() for i in ml: print(i)
##10-2 C 语言学习笔记 :可使用方法 replace() 将字符串中的特定单词都替换为另一个单词。
读取你刚创建的文件 learning_python.txt 中的每一行,将其中的 Python 都替换为另一门语言的名称,如 C 。将修改后的各行都打印到屏幕上。
1 2 3 4 5 6
file = 'learning_python.txt' with open(file, 'r', encoding='UTF-8') as f: ml = f.readlines() for i in ml: i = i.replace('Python', 'C') print(i)
a = input('Enter the first number:\n') b = input('Enter the second number:\n') try: print(int(a) + int(b)) except TypeError as e: print("TypeError!\n", e)
##10-7 加法计算器 :将你为完成练习 10-6 而编写的代码放在一个 while 循环中,让用户犯错(输入的是文本而不是数字)后能够继续输入数字。
1 2 3 4 5 6 7
whileTrue: a = input('Enter the first number:\n') b = input('Enter the second number:\n') try: print(int(a) + int(b)) except TypeError as e: print("TypeError!\n", e)
f1 = 'cats.txt ' f2 = 'dogs.txt' try: with open(f1, 'r') as f: print(f.read()) except FileNotFoundError as e: print(e) try: with open(f2, 'r') as f: print(f.read()) except FileNotFoundError as e: print(e)
import json f1 = 'num.json' n = input("Enter a number:\n") with open(f1, 'w') as f: json.dump(n, f) with open(f1) as f: nn = json.load(f) print("I know your favorite number! It's {}.".format(nn))
import json f1 = 'num.json' try: with open(f1) as f: nn = json.load(f) print("I know your favorite number! It's {}.".format(nn)) except FileNotFoundError as e: n = input("Enter a number:\n") with open(f1, 'w') as f: json.dump(n, f)
username = input("What is your name? ") filename = 'username.json' with open(filename, 'w') as f_obj: json.dump(username, f_obj) return username
defgreet_user(): """ 问候用户,并指出其名字 """ a = input("What's your name?\n") if a == get_stored_username(): username = get_stored_username() if username: print("Welcome back, " + username + "!") else: username = get_new_username() print("We'll remember you when you come back, " + username + "!") else: username = get_new_username() if username: print("Welcome back, " + username + "!") else: username = get_new_username() print("We'll remember you when you come back, " + username + "!")