1. 程式人生 > >Python作業(10.1-10.12)

Python作業(10.1-10.12)

Python第10章作業


10-1 Python學習筆記 : 在文字編輯器中新建一個檔案, 寫幾句話來總結一下你至此學到的Python知識, 其中每一行都以“In Python you can”打頭。 將這個檔案命名為learning_python.txt, 並將其儲存到為完成本章練習而編寫的程式所在的目錄中。 編寫一個程式, 它讀取這個檔案, 並將你所寫的內容列印三次: 第一次列印時讀取整個檔案; 第二次列印時遍歷檔案物件; 第三次列印時將各行儲存在一個列表中, 再在with 程式碼塊外列印它們。

with open('learning_python.txt') as file_object1:
	contents=file_object1.read()
	print(contents)
	
filename='learning_python.txt'
with open(filename) as file_object2:
	for line in file_object2:
		print(line)
		
with open(filename) as file_object3:
	lines=file_object3.readlines()
for line in lines:
	print(line)



10-2 C語言學習筆記 : 可使用方法replace() 將字串中的特定單詞都替換為另一個單詞。 下面是一個簡單的示例, 演示瞭如何將句子中的'dog' 替換為'cat'
>>> message = "I really like dogs."
>>> message.replace('dog', 'cat')
'I really like cats.'

讀取你剛建立的檔案learning_python.txt中的每一行, 將其中的Python都替換為另一門語言的名稱, 如C。 將修改後的各行都列印到螢幕上。

filename='learning_python.txt'
with open(filename) as file_object3:
	lines=file_object3.readlines()
	
for line in lines:
	line=line.replace('Python','C')
	print(line)



10-3 訪客 : 編寫一個程式, 提示使用者輸入其名字; 使用者作出響應後, 將其名字寫入到檔案 guest.txt 中。
name=input("Please input your name\n")
filename='guest.txt'
with open(filename,'w') as file_object:
	file_object.write(name)



10-6 加法運算 : 提示使用者提供數值輸入時, 常出現的一個問題是, 使用者提供的是文字而不是數字。 在這種情況下, 當你嘗試將輸入轉換為整數時, 將引發TypeError 異常。 編寫一個程式, 提示使用者輸入兩個數字, 再將它們相加並列印結果。 在使用者輸入的任何一個值不是數字時都捕獲

TypeError (ValueError異常, 並列印一條友好的錯誤訊息。 對你編寫的程式進行測試: 先輸入兩個數字, 再輸入一些文字而不是數字。

print("Please input two numbers ")
try:
	num1=input("The first number is :")
	num1=int(num1)
except ValueError:
	print("Sorry,your input is illegal")
try:
	num2=input("The second number is:")
	num2=int(num2)
except ValueError:
	print("Sorry,your input is illegal")
else:
	num=num1+num2
	print(num)


10-7 加法計算器 : 將你為完成練習10-6而編寫的程式碼放在一個while 迴圈中, 讓使用者犯錯(輸入的是文字而不是數字) 後能夠繼續輸入數字。

print("Please input two numbers ")
while(True):
	try:
		num1=input("The first number is :")
		num1=int(num1)
	except ValueError:
		continue 
	try:
		num2=input("The second number is:")
		num2=int(num2)
	except ValueError:
		continue
	else:
		num=num1+num2
		print(num)


10-11 喜歡的數字 : 編寫一個程式, 提示使用者輸入他喜歡的數字, 並使用json.dump() 將這個數字儲存到檔案中。 再編寫一個程式, 從檔案中讀取這個值, 並列印訊息“I know your favorite number! It's _____.”

import json
favorite_num=input("What is your favorite number?")
favorite_num=int(favorite_num)

filename='number.json'
with open(filename,'w') as fobj:
	json.dump(favorite_num,fobj)
	print("OK,I will remember it") 

import json
filename='number.json'
with open(filename) as fobj:
	usernum=json.load(fobj)
	print("Welcome back , your favorite_number is "+str(usernum))


10-12 記住喜歡的數字 : 將練習10-11中的兩個程式合而為一。 如果儲存了使用者喜歡的數字, 就向用戶顯示它, 否則提示使用者輸入他喜歡的數字並將其儲存到檔案中。執行這個程式兩次, 看看它是否像預期的那樣工作。

import json
filename='num.json'

def get_sorted_number():
	try:
		with open(filename) as f_obj:
			usernum=json.load(f_obj)
	except FileNotFoundError:
		return None
	else:
		return usernum

def greet_user():
	usernum=get_sorted_number()
	if usernum:
		print("Welcome back,your number is "+str(usernum))
	else:
		usernum=input("What is your favorite number?")
		usernum=int(usernum)
		with open(filename,'w') as f_obj:
			json.dump(usernum,f_obj)
			print("I will remember this number")

greet_user()