20211106,20211107
suspended 2 days for tinkering my desktop.I've bought a 16Gbytes Samsung SDRAM, that's so great.10-1withopen('learning_python.txt')as file_object:
contents = file_object.read()print(contents)print('\n')withopen('learning_python.txt')as file_object:for line in file_object:print(line.rstrip())print('\n')withopen('learning_python.txt')as file_object:
l_p =[]for line in file_object:
l_p.append(line.rstrip())for l in l_p:print(l)10-2withopen('learning_python.txt')as file_object:
contents =[]for line in file_object:
contents.append(line.replace('Python','C'))for c in contents:print(c.rstrip())10-3withopen('guest.txt','w')as file:print('enter 1 to quit')while True:
guest =input('plz enter ur name:')if guest =='1':breakelse:
file.write(guest +'\n')10-4import time
withopen('guest.txt','w')as file:print('enter q to quit')while True:
guest =input('plz enter ur name:')if guest =='q':breakelse:
file.write(guest +': '+ time.strftime('%Y-%m-%d %H:%M:%S',\
time.localtime())+'\n')print("What's up! "+ guest.title())10-5withopen('reason.txt','a')as file:print('enter q to quit')while True:
reason =input('why do u like coding:')if reason =='q':breakelse:
file.write(reason +'\n')10-6print('\nenter 2 numbers, I will add them ')
num1 =input('first number: ')
num2 =input('second number: ')try:
num =float(num1)+float(num2)
except ValueError:print('u can only input numbers')else:print('\nanswer: '+str(num))10-7while True:print('\nenter 2 numbers, I will add them ')print('enter "q" to quit')
num1 =input('first number: ')if num1 =='q':breakelse:
num2 =input('second number: ')try:
num =float(num1)+float(num2)
except ValueError:print('u can only input numbers')else:print('\nanswer: '+str(num))10-8try:withopen('cats.txt')as f1:
contents1 = f1.read()print('cats:\n'+ contents1)withopen('dogs.txt')as f2:
contents2 = f2.read()print('\ndogs:\n'+ contents2)
except FileNotFoundError:print('u should put files in the same place of this .py')10-9try:withopen('cats.txt')as f1:
contents1 = f1.read()print('cats:\n'+ contents1)withopen('dogs.txt')as f2:
contents2 = f2.read()print('\ndogs:\n'+ contents2)
except FileNotFoundError:
pass
-------------------------------------------------------------------------------REVISED:
filenames =['cats.txt','dogs.txt']for filename in filenames:try:withopen(filename)as f:
contents = f.read()
except FileNotFoundError:
pass
else:print("\nReading file: "+ filename)print(contents)-------------------------------------------------------------------------------10-10withopen('Hard Times.txt','r', encoding='utf-8')as f:
#---------------------------------------------,'r', encoding='utf-8 is pivotal
c = f.read()
n = c.lower().count('hard')print("nums of 'the': "+str(n))10-11import json
num =int(input('plz enter ur favorite number:'))withopen('favorite_number.json','w')as f:
json.dump(num, f)withopen('favorite_number.json')as f:
nums = json.load(f)print(nums)10-12import json
try:withopen('george.json')as f:
num = json.load(f)
except FileNotFoundError:withopen('george.json','w')as f:
num =int(input('plz enter ur favorite number:'))
json.dump(num, f)else:print('ur favorite num is '+str(num))10-13import json
def get_stored_username():"""Get stored username if available."""
filename ='username.json'try:withopen(filename)as f_obj:
username = json.load(f_obj)
except FileNotFoundError:return None
else:return username
def get_new_username():"""Prompt for a new username."""
username =input("What is your name? ")
filename ='username.json'withopen(filename,'w')as f_obj:
json.dump(username, f_obj)return username
def verify(username):
man =input('are u '+ username.title()+' ?(y/n)')if man =='y':print("Welcome back, "+ username +"!")else:
username =get_new_username()print("We'll remember you when you come back, "+ username +"!")
def greet_user():"""Greet the user by name."""
username =get_stored_username()if username:verify(username)else:
username =get_new_username()print("We'll remember you when you come back, "+ username +"!")greet_user()