1、代码学习部分
# #python学习笔记
with open ('learning_python.txt')as file_object:
contents=file_object.read()
print(contents.rstrip())
filename='learning_python.txt'
with open (filename)as file_object:
for line in file_object:
print(line.rstrip())
filename='learning_python.txt'
with open (filename)as file_object:
lines=file_object.readlines()
for line in lines:
print(line.rstrip())
# #Java语言学习笔记
filename='learning_python.txt'
with open (filename)as file_object:
lines=file_object.readlines()
learning_string=''
for line in lines:
learning_string+=line.replace('Python','Java')
print(learning_string)
#10-3 访客:
name_file=input("Please input your name: ")
with open('guest.txt','w') as file_object:
file_object.write(name_file+"\n")
#10-4 访客名单:
while(True):
name_file = input("Please input your name: ")
if name_file == 'quit':
break
print("Good morning, "+name_file)
print("Please input 'quit' to stop input.")
with open('guest.txt', 'a') as file_object:
file_object.write(name_file+" just login in."+'\n')
#10-5关于编程的调查:
while(True):
reason=input("Please tell me the reason why you love python: ")
if reason == 'quit':
break
print("Please input 'quit' to stop input.")
with open('reason.txt', 'a') as file_object:
file_object.write(reason+'\n')
#10-6 加法运算
print("Give me two numbers, and I'll plus them.")
number1=input("Please input the first number: ")
try:number1=int(number1)
except ValueError:
print("Your input is not number.\nPlease check your input.")
number2=input("Please input the second number: ")
try:
number2 = int(number2)
except ValueError:
print("Your input is not number.\nPlease check your input.")
result=int(number1)+int(number2)
print("Result is: "+str(result)) #这里print中result是数值,要用str改为字符串才可输出
#10-7 加法计算器:
print("Give me two numbers, and I'll plus them.")
print("Enter 'q' to quit.")
while(True):
number1=input("Please input the first number: ")
if number1=='q':
break
number2=input("Please input the second number: ")
if number2=='q':
break
try:
result=int(number1)+int(number2)
except ValueError:
print("Your input is not number.\nPlease check your input.")
else:
print(result)
#猫和狗
try:
filename='cats.txt'
with open (filename)as file_object:
for line in file_object:
print(line.rstrip())
filename='dogs.txt'
with open (filename)as file_object:
for line in file_object:
print(line.rstrip())
except FileNotFoundError:
print("File is not found.")
#沉默的猫和狗
try:
filename='cats.txt'
with open (filename)as file_object:
# for line in file_object:
print(line.rstrip())
filename='dogs.txt'
with open (filename)as file_object:
for line in file_object:
print(line.rstrip())
except FileNotFoundError:
pass
#常见单词:
filename='book.txt'
try:
with open(filename)as file_object:
contents=file_object.read()
print(contents.count('the'))
except FileNotFoundError:
print("File is not found.")
#10-11喜欢的数字:
import json
favorite_number=input("Please input your favorite number: ")
filename='favorite_number.json'
with open(filename,'w')as file_object:
json.dump(favorite_number,file_object)
with open(filename)as file_object:
favorite_number=json.load(file_object)
print("I know your favourite number!It's "+favorite_number)
#10-12 记住喜欢的数字
import json
filename='favorite_number.json'
try:
with open(filename)as file_object:
favorite_number=json.load(file_object)
except FileNotFoundError:
favorite_number=input("Please input your favorite number: ")
with open(filename,'w')as file_object:
json.dump(favorite_number,file_object)
print("We will remember your favourite number when you come back!")
else:
print("I know your favourite number!It's " + favorite_number)
#10-13 验证用户
import json
def get_stored_username():
"""如果存储了用户名,就获取它"""
filename='username.json'
try:
with open(filename)as file_object:
username=json.load(file_object)
except FileNotFoundError:
return None
else:
comfirm=input('If your name is '+username+'?(y/n)')
if comfirm=='y':
return username
else:
username=get_new_username()
def get_new_username():
"""提示用户输入用户名"""
username=input("What's your name?")
filename='username.json'
with open(filename,'w')as file_object:
json.dump(username,file_object)
return username
def greet_user():
"""问候用户,并指出其名字"""
username=get_stored_username()
if username:
print("Welcom back, "+username+'!')
else:
username=get_new_username()
print("We'll remember you when you come back, "+username+'!')
greet_user()
#10-13 验证用户
import json
def get_stored_username():
filename = 'username.json'
with open(filename) as f_obj:
username = f_obj.read()
return username
def get_new_username():
username = input("What is your name? ")
filename = 'username.json'
with open(filename, 'w') as f_obj:
json.dump(username, f_obj)
return username
def greet_user():
username = get_stored_username()
username = str(username)
if username:
yep=input('Is ' + username + ' your name?(y/n)')
print (yep)
if yep == '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()