python作业(9)

10-1 Python 学习笔记:

    代码如下:

filename='PythonLearn.txt'
with open(filename) as F:
	contents=F.read()
	print(contents)

with open(filename) as F:
	for line in F:
		print(line.rstrip())

with open(filename) as F:
	lines=F.readlines()

for line in lines:
		print(line.rstrip())



    运行结果:

    python作业(9)_第1张图片


10-2 C语言学习笔记:

    代码如下:

filename='PythonLearn.txt'


with open(filename) as F:
	for line in F:
		print(line.rstrip())

with open(filename) as F:
	lines=F.readlines()

for line in lines:
	line=str(line).replace('Python','C')
	print(line.rstrip())



    结果如下:

    python作业(9)_第2张图片


10-4 访客名单:

     代码如下:

filename='guest_book.txt'
name=input("What's your name? (input 'Q' to quit)\n")
while(name!='Q'):
	with open(filename , 'a') as fob:
		fob.write(name+" has logged in. \n")
		print("Hello, "+name)
	name=input("What's your name? (input 'Q' to quit)\n")



    结果如下:

    python作业(9)_第3张图片

    python作业(9)_第4张图片


10-6 加法运算:

    代码如下:

n=input("Please input two numbers and separate by a blank.\n")
nums=n.split()
try:
	ans=float(nums[0])+float(nums[1])
	ans=round(ans,2)
except ValueError:
	print("Oh, what you input isn't a number.")
else:
	print("The sum is "+str(ans)+".\n")


    结果如下:

    python作业(9)_第5张图片

    python作业(9)_第6张图片


10-7 加法计算器:

    代码如下:

i='yes'
while(i=='yes'):
	n=input("Please input two numbers and separate by a blank.\n")
	nums=n.split()
	try:
		ans=float(nums[0])+float(nums[1])
		ans=round(ans,2)
	except ValueError:
		print("Oh, what you input isn't a number.")
	else:
		print("The sum is "+str(ans)+".\n")
	i=input("Please input 'yes' if you want to continue.")


    结果如下:

    python作业(9)_第7张图片

10-11 喜欢的数字:

    代码如下:

import json
filename='num.json'
num=input("What's your favourite number?\n")
with open(filename,'w') as obj:
	json.dump(num,obj)
	print("Ok, I remember it.\n")
import json
filename='num.json'
with open(filename) as obj:
	num=json.load(obj)
	print("I remenber that your favourite number is "+num+".\n")



    运行结果:

    python作业(9)_第8张图片

    python作业(9)_第9张图片


10-12 记住喜欢的数字:

    代码如下:

import json
filename='num.json'
def get_num():	
	try:
		with open(filename) as obj:
			num=json.load(obj)
	except FileNotFoundError:
		return None
	else:
		return num

def new_num( ):
	num=input("What's your favourite number?\n")
	with open(filename,'w') as obj:
		json.dump(num,obj)
		print("Ok, I remember it.\n")
	return num

def favourite_num():
	num=get_num()
	if num:
		print("I remenber that your favourite number is "+num+".\n")
	else:
		num=new_num()

favourite_num()



    结果如下:

    python作业(9)_第10张图片

     python作业(9)_第11张图片


你可能感兴趣的:(python作业(9))