《Python程式設計》第十章部分課後練習題

Zetrue_Li發表於2018-04-05

#10-3 訪客:

程式碼:

#10-3 訪客

def save(name):
	"""save user's name in file"""
	file_name = 'guest.txt'
	with open(file_name, 'a') as fobj:
		fobj.write(name+'\n')
	
while True:
	message = "Hello, please input your name('q' to quit): "
	name = input(message)
	if name != 'q':
		save(name)
	else:
		break

輸出:pass


#10-4 訪客名單:

程式碼:

#10-4 訪客名單
import datetime

def save(name):
	"""save user's name in file"""
	file_name = 'guest_book.txt'
	with open(file_name, 'a') as fobj:
		message = name.title() + " visited the web on "
		message += datetime.datetime.now().strftime("%Y.%m.%d-%H:%M:%S")
		fobj.write(message+'\n')
	
while True:
	message = "Hello, please input your name('q' to quit): "
	name = input(message)
	if name != 'q':
		save(name)
		message = "Welcome, Dear " + name.title() + '.'
		print(message)
	else:
		break

輸出:pass


#10-6 加法運算:

程式碼:

#10-6 加法運算

def add(a, b):
	"""add tow numbers together"""
	try:
		c = int(a) + int(b)
		print("The result is " + str(c))
	except ValueError:
		message = "You did not input the vaild integer."
		print(message)

while True:
	message = "Please input the first integer ('q' to quit): "
	a = input(message)
	if(a=='q'):
		break;
	message = "Please input the second integer ('q' to quit): "
	b = input(message)
	if(b=='q'):
		break;
	add(a, b)

輸出:

Please input the first integer ('q' to quit): 1
Please input the second integer ('q' to quit): 2
The result is 3
Please input the first integer ('q' to quit): 1
Please input the second integer ('q' to quit): a
You did not input the vaild integer.
Please input the first integer ('q' to quit): q


#10-7 加法計算器:

程式碼:

#10-7 加法計算器

def add(a, b):
	"""add tow numbers together"""
	try:
		c = int(a) + int(b)
		print("The result is " + str(c))
	except ValueError:
		message = "You did not input the vaild integer."
		print(message)

while True:
	message = "Please input two valid integers ('q' to quit): "
	line = input(message)
	if line == 'q':
		break;
	l = line.split()
	a = l[0]
	b = l[-1]
	add(a, b)

輸出:

Please input two valid integers ('q' to quit): 1 2
The result is 3
Please input two valid integers ('q' to quit): hahaha
You did not input the vaild integer.
Please input two valid integers ('q' to quit): a
You did not input the vaild integer.
Please input two valid integers ('q' to quit): a b
You did not input the vaild integer.
Please input two valid integers ('q' to quit): q


#10-12 記住喜歡的數字:

程式碼:

#10-12 記住喜歡的數字
import json

def save():
	"""save the num in file"""
	num = input("What is your number: ")
	try:
		temp = int(num)
	except ValueError:
		message = 'You did not input the valid number.'
		print(message)
	else:
		file_name = "Love_num.txt"
		with open(file_name, 'w') as fobj:
			json.dump(num, fobj)

def load():
	"""load the num from file"""
	file_name = "Love_num.txt"
	try:
		with open(file_name, 'r') as fobj:
			num = json.load(fobj)
	except FileNotFoundError:
		return None
	else:
		return num

def guess_num():
	"""guess the num inputed by user"""
	num = load()
	if num:
		message = "I know your favorite number! It's " + num + "."
		print(message)
	else:
		save()
		
guess_num()
guess_num()
輸出:pass

相關文章