《Python编程 从入门到实践》第十章习题选做
2018-04-04 22:16
423 查看
10-1 Python学习笔记
在learning_python.txt文件:In Python you can learn how to program.
In Python you can feel every interested.
In Python you can learn how to solve hard problem.
In Python you can feel every interested.
In Python you can learn how to solve hard problem.
**********************
In Python you can learn how to program.
In Python you can feel every interested.
In Python you can learn how to solve hard problem.
**********************
In Python you can learn how to program.
In Python you can feel every interested.
In Python you can learn how to solve hard problem.10-2 C语言学习笔记filename = 'learning_python.txt'
with open(filename) as f:
lines = f.readlines()
for line in lines:
print(line.replace('Python', 'C').rstrip())输出:In C you can learn how to program.
In C you can feel every interested.
In C you can learn how to solve hard problem.10-3 访客名单filename = 'guest_book.txt'
with open(filename, 'w') as f_obj:
while True:
print("input 'quit' to exit.")
username = input("Please inout your name: ")
if username == 'quit':
break
print("Hello " + username + ".")
f_obj.write(username + " has already visited.\n")输出:input 'quit' to exit.
Please inout your name: Li Hua
Hello Li Hua.
input 'quit' to exit.
Please inout your name: John
Hello John.
input 'quit' to exit.
Please inout your name: Amy
Hello Amy.
input 'quit' to exit.
Please inout your name: quit文件guest_book.txt:Li Hua has already visited.
John has already visited.
Amy has already visited.
10-7 加法计算器print("Give me two numbers, and I'll add them.")
print("Enter'q' to quit.")
msg = "Please input two numbers, and separated by space.\n"
while True:
try:
str = input(msg)
if str == 'q':
break
first, second = map(int, str.split())
except ValueError:
print("Please enter two pure numbers.")
else:
print(first + second)输出:Give me two numbers, and I'll add them.
Enter'q' to quit.
Please input two numbers, and separated by space.
we 34
Please enter two pure numbers.
Please input two numbers, and separated by space.
23 rt
Please enter two pure numbers.
Please input two numbers, and separated by space.
sd er
Please enter two pure numbers.
Please input two numbers, and separated by space.
34 78
112
Please input two numbers, and separated by space.
q10-8 猫和狗def print_animals_name(filename):
try:
with open(filename) as f_obj:
lines = f_obj.readlines()
except FileNotFoundError:
msg = "Sorry, the file " + filename + " doesn't exist."
print(msg)
else:
for line in lines:
print(line.rstrip())
filenames = ['cats.txt', 'dogs.txt']
for filename in filenames:
print(filename + ": ")
print_animals_name(filename)两个文件都存在时的输出:cats.txt:
Groot
Cesar
Wendy
Baccano
dogs.txt:
Pitt
Dick
Mike
Nick
Alicecats.txt不存在时的输出:cats.txt:
Sorry, the file cats.txt doesn't exist.
dogs.txt:
Pitt
Dick
Mike
Nick
Alice10-9 沉默的猫和狗def print_animals_name(filename):
try:
with open(filename) as f_obj:
lines = f_obj.readlines()
except FileNotFoundError:
pass
# msg = "Sorry, the file " + filename + " doesn't exist."
# print(msg)
else:
for line in lines:
print(line.rstrip())
filenames = ['cats.txt', 'dogs.txt']
for filename in filenames:
print(filename + ": ")
print_animals_name(filename)cats.txt不存在:cats.txt:
dogs.txt:
Pitt
Dick
Mike
Nick
Alice10-10 常见单词filename = 'Pennsylvania Journal.txt'
with open(filename) as f_obj:
contents = f_obj.read()
num = contents.lower().count('the')
print(num)输出:266810-11 喜欢的数字import json
filename = 'favorite_number.json'
with open(filename, 'w') as f_obj:
number = input("Please input your favorite number; ")
json.dump(number, f_obj)
with open(filename) as f_obj:
number = json.load(f_obj)
msg = "I know your favorite number!"
print(msg + " It's " + str(number))输出:Please input your favorite number; 34
I know your favorite number! It's 3410-12 记住喜欢的数字import json
filename = 'favorite_number.json'
test = 2
while test != 0:
test -= 1
try:
with open(filename) as f_obj:
number = json.load(f_obj)
except FileNotFoundError:
with open(filename, 'w') as f_obj:
number = input("Please input your favorite number; ")
json.dump(number, f_obj)
else:
msg = "I know your favorite number!"
print(msg + " It's " + str(number))输出:Please input your favorite number; 56
I know your favorite number! It's 56
在learning_python.txt文件:In Python you can learn how to program.
In Python you can feel every interested.
In Python you can learn how to solve hard problem.
filename = 'learning_python.txt' with open(filename) as f: s = f.read() print(s) print("**********************") with open(filename) as f: for line in f: print(line.rstrip()) print("**********************") with open(filename) as f: lines = f.readlines() for line in lines: print(line.rstrip())输出:In Python you can learn how to program.
In Python you can feel every interested.
In Python you can learn how to solve hard problem.
**********************
In Python you can learn how to program.
In Python you can feel every interested.
In Python you can learn how to solve hard problem.
**********************
In Python you can learn how to program.
In Python you can feel every interested.
In Python you can learn how to solve hard problem.10-2 C语言学习笔记filename = 'learning_python.txt'
with open(filename) as f:
lines = f.readlines()
for line in lines:
print(line.replace('Python', 'C').rstrip())输出:In C you can learn how to program.
In C you can feel every interested.
In C you can learn how to solve hard problem.10-3 访客名单filename = 'guest_book.txt'
with open(filename, 'w') as f_obj:
while True:
print("input 'quit' to exit.")
username = input("Please inout your name: ")
if username == 'quit':
break
print("Hello " + username + ".")
f_obj.write(username + " has already visited.\n")输出:input 'quit' to exit.
Please inout your name: Li Hua
Hello Li Hua.
input 'quit' to exit.
Please inout your name: John
Hello John.
input 'quit' to exit.
Please inout your name: Amy
Hello Amy.
input 'quit' to exit.
Please inout your name: quit文件guest_book.txt:Li Hua has already visited.
John has already visited.
Amy has already visited.
10-7 加法计算器print("Give me two numbers, and I'll add them.")
print("Enter'q' to quit.")
msg = "Please input two numbers, and separated by space.\n"
while True:
try:
str = input(msg)
if str == 'q':
break
first, second = map(int, str.split())
except ValueError:
print("Please enter two pure numbers.")
else:
print(first + second)输出:Give me two numbers, and I'll add them.
Enter'q' to quit.
Please input two numbers, and separated by space.
we 34
Please enter two pure numbers.
Please input two numbers, and separated by space.
23 rt
Please enter two pure numbers.
Please input two numbers, and separated by space.
sd er
Please enter two pure numbers.
Please input two numbers, and separated by space.
34 78
112
Please input two numbers, and separated by space.
q10-8 猫和狗def print_animals_name(filename):
try:
with open(filename) as f_obj:
lines = f_obj.readlines()
except FileNotFoundError:
msg = "Sorry, the file " + filename + " doesn't exist."
print(msg)
else:
for line in lines:
print(line.rstrip())
filenames = ['cats.txt', 'dogs.txt']
for filename in filenames:
print(filename + ": ")
print_animals_name(filename)两个文件都存在时的输出:cats.txt:
Groot
Cesar
Wendy
Baccano
dogs.txt:
Pitt
Dick
Mike
Nick
Alicecats.txt不存在时的输出:cats.txt:
Sorry, the file cats.txt doesn't exist.
dogs.txt:
Pitt
Dick
Mike
Nick
Alice10-9 沉默的猫和狗def print_animals_name(filename):
try:
with open(filename) as f_obj:
lines = f_obj.readlines()
except FileNotFoundError:
pass
# msg = "Sorry, the file " + filename + " doesn't exist."
# print(msg)
else:
for line in lines:
print(line.rstrip())
filenames = ['cats.txt', 'dogs.txt']
for filename in filenames:
print(filename + ": ")
print_animals_name(filename)cats.txt不存在:cats.txt:
dogs.txt:
Pitt
Dick
Mike
Nick
Alice10-10 常见单词filename = 'Pennsylvania Journal.txt'
with open(filename) as f_obj:
contents = f_obj.read()
num = contents.lower().count('the')
print(num)输出:266810-11 喜欢的数字import json
filename = 'favorite_number.json'
with open(filename, 'w') as f_obj:
number = input("Please input your favorite number; ")
json.dump(number, f_obj)
with open(filename) as f_obj:
number = json.load(f_obj)
msg = "I know your favorite number!"
print(msg + " It's " + str(number))输出:Please input your favorite number; 34
I know your favorite number! It's 3410-12 记住喜欢的数字import json
filename = 'favorite_number.json'
test = 2
while test != 0:
test -= 1
try:
with open(filename) as f_obj:
number = json.load(f_obj)
except FileNotFoundError:
with open(filename, 'w') as f_obj:
number = input("Please input your favorite number; ")
json.dump(number, f_obj)
else:
msg = "I know your favorite number!"
print(msg + " It's " + str(number))输出:Please input your favorite number; 56
I know your favorite number! It's 56
相关文章推荐
- 《Python编程——从入门到实践》第十章部分习题解
- python编程:从入门到实践 第十章习题
- 《Python编程 从入门到实践》第十章课后习题
- 《Python编程-从入门到实践》第九章及第十章习题训练
- Python编程从入门到实践:习题5-3~5-7
- Python编程从入门到实践:习题6-4~6-6
- 《Python编程-从入门到实践》第五章及第六 a067 章习题训练
- Python编程:从入门到实践 第六章习题
- 《Python编程——从入门到实践》第七章部分习题解
- Python编程:从入门到实践(课后习题11)
- Python编程从入门到实践习题5
- 《Python编程 从入门到实践》第二章习题选做
- Python编程:从入门到实践的动手试一试答案(第十章)
- 《Python编程-从入门到实践》课后习题(2)
- 《Python编程 从入门到实践》第四章习题选做
- 《Python编程 从入门到实践》第八章习题选做
- Python编程:从入门到实践(课后习题10)
- 《python编程从入门到实践》 第9章习题选做
- [Python编程:从入门到实践] 第十六章:下载数据 习题答案
- 《Python编程——从入门到实践》第五章部分习题解