第十次作業(2018-04-04,週三)
教材第十章習題
10-1
#learning_python.txt
In python you can do mathematical calculations
In python you can keep accounts
In python you can design websites
In python you can develop good games
#test.py
file_name = "learning_python.txt"with open(file_name) as file_object:
contents = file_object.read()
print(contents.rstrip())
with open(file_name) as file_object:
for line in file_object:
print(line.rstrip())
with open(file_name) as file_object:
lines = file_object.readlines()
for line in lines:
print(line.rstrip())
10-2
file_name = "learning_python.txt"
with open(file_name) as file_object:
contents = file_object.read()
print(contents.replace("python","C").rstrip())
10-3
name = input("What's your name?")
file_name = "guest.txt"
with open(file_name,"w") as file_object:
file_object.write(name)
10-4
file_name = "guest_book.txt"
while True:
name = input("What's your name?(q to quit)")
if name != 'q':
print("Hello, "+name+"!")
with open(file_name,'a') as file_object:
file_object.write(name+"\n")
else:
break
10-5
file_name = "programing_reason.txt"
while True:
reason = input("Why do like programing?(q to quit)")
if reason != "q":
with open(file_name,'a') as file_object:
file_object.write(reason+'\n')
else:
break
10-6
number1 = (input("Enter the first number:"))
number2 = (input("Enter the second number:"))
try:
number1 = int(number1)
number2 = int(number2)
print(number1+number2)
except ValueError:
print("Input invalid!")
10-7
while True:
number1 = (input("Enter the first number:"))
number2 = (input("Enter the second number:"))
try:
number1 = int(number1)
number2 = int(number2)
except ValueError:
print("Input invalid!")
else:
print(number1+number2)
break
10-8
file_list = ["cats.txt", "dogs.txt"]
def print_file(file_name):
try:
with open(file_name) as file_object:
print(file_object.read().rstrip())
except FileNotFoundError:
print("The file "+file_name+" doesn't exist.")
for file in file_list:
print_file(file)
10-9
file_list = ["cats.txt", "dogs.txt"]
def print_file(file_name):
try:
with open(file_name) as file_object:
print(file_object.read().rstrip())
except FileNotFoundError:
pass
for file in file_list:
print_file(file)
10-11
#test1.py
import json
favorite_number = input("What's your favorite number?")
file_name = "favorite_number"
with open(file_name,'w') as file_object:
json.dump(favorite_number,file_object)
#test2.py
import json
file_name = "favorite_number"
with open(file_name) as file_object:
favorite_number = json.load(file_object)
print("I know your favorite number!It's "+favorite_number+".")
10-12
import json
file_name = "favorite_number.txt"
try:
with open(file_name) as file_object:
favorite_number = json.load(file_object)
except FileNotFoundError:
favorite_number = input("What's your favorite number?")
with open(file_name,'w') as file_object:
json.dump(favorite_number, file_object)
else:
print("I know your favorite number!It's "+favorite_number+".")
10-13
import json
def get_stored_username():
file_name = 'username.json'
try:
with open(file_name) as file_object:
username = json.load(file_object)
except FileNotFoundError:
return None
else:
return username
def get_new_username():
username = input("What's your name?")
file_name = 'username.json'
with open(file_name,'w') as file_object:
json.dump(username, file_object)
return username
def greet_user():
username = get_stored_username()
if username:
answer = input("Your name is "+username+', right?(y or n)')
if answer == 'y':
print("Welcome back, "+username+"!")
else:
username = get_new_username()
print("We'll remember you when you come back, "+username+"!")
else:
username = get_new_username()
print("We'll remember you when you come back, "+username+"!")
greet_user()
相關文章
- 第三週作業
- 第三週 作業3
- 第三週作業2
- 第三週 作業4
- Python第三週作業Python
- Javaweb第三週作業JavaWeb
- 【團隊作業】第三週作業1
- 馬哥教育第三週作業
- 第九組【團隊作業】第三週作業4
- 第六組【團隊作業】第三週作業4
- 第六組【團隊作業】第三週作業2
- 第五組【團隊作業】第三週作業1
- 第八組【團隊作業】第三週作業3
- 第八組【團隊作業】第三週作業1
- 第一組【團隊作業】第三週作業2
- 第七組【團隊作業】第三週作業4
- 厚基礎Linux——第三週作業Linux
- 計算機課程第三週作業計算機
- 高階語言程式設計第十次作業程式設計
- 高階語言程式設第十次個人作業
- 第三組【團隊作業】第二週使用者調研作業2
- 高階語言程式設計課程第十次作業程式設計
- 高階語言程式設計第十次個人作業程式設計
- 高階語言程式課程第十次個人作業
- 第八週作業
- 第七週作業
- 第二週作業
- 作業三
- 高階語言程式設計課程第十次個人作業程式設計
- 高階程式語言設計課程第十次個人作業
- 第二週作業0414
- jsp第七週作業JS
- 第一週作業補
- CINTA 作業三
- 第六組【團隊作業】第六週作業
- 第八組【團隊作業】第六週作業
- 第五組【團隊作業】第六週作業
- JAVA EE 第一週作業Java