Python编程从入门到实践第10章习题答案

#10-1Python学习笔记
file_path = 'E:\python_work\example\learing_python.txt'
with open(file_path) as file_object:
	#print(file_object.read())
	
	lines = file_object.readlines()
	# for line in lines:
		# print(line.rstrip())

string = ''
for line in lines:
	string += line.rstrip()	
print(string) 

#10-2 C语言学习笔记
string = ''
for line in lines:
	string += line.replace('Python','C')
print(string)


#10-4访客名单
user_infor = input('Please enter your name: ')
print("Hello, " + user_infor.title())

file_path = 'E:\python_work\example\guest_book.txt'
with open(file_path,'a') as file_object:
	file_object.write(user_infor.title() + 'has a visist your space\n')

#10-5关于编程的调查
filename = 'reason_book.txt'

responses = []
while True:
    response = input("\nWhy do you like programming? ")
    responses.append(response)

    continue_poll = input("Would you like to let someone else respond? (y/n) ")
    if continue_poll != 'y':
        break

with open(filename, 'a') as f:
    for response in responses:
        f.write(response + "\n")

#10-6加法运算
try:
	a = input('Enter the first number: ')
	a = int(a)
	b = input('Enter the second number: ')
	b = int(b)
except ValueError:
	print("a or b doesn't a math number.")
else:
	answer = a + b
	print('the answer is: ' + str(answer))
#10-7加法计算器
print("Enter 'q' at any time to quit.\n")
while True:
	try:
		a = input('Enter the first number: ')
		if a == 'q':
			break
		a = int(a)
		b = input('Enter the second number: ')
		if b == 'q':
			break
		b = int(b)
	except ValueError:
		print("a or b doesn't a math number.")
	else:
		answer = a + b
		print('the answer is: ' + str(answer))
		

你可能感兴趣的:(Python编程从入门到实践第10章习题答案)