第十次作業(2018-04-04,週三)

huanghh29發表於2018-04-08

教材第十章習題

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()

相關文章