10-3
name=input("输入你的名字:") with open('guest.txt','a') as file_object: ?? ?file_object.write('\n'+name)
10-4
while True: ?? ?message=input("输入你的名字:\n输入q退出。") ?? ?if message=='q': ?? ??? ?break ?? ?print(f"Hello, {message.title()}!") ?? ?with open('guest_book.txt','a') as file_object: ?? ??? ?file_object.write(message+"\n")
10-5
while True: ?? ?reason=input("Why do you love code?\nEnter 'q' to exit.") ?? ?if reason=='q': ?? ??? ?break ?? ?with open('reasons.txt','a') as file_object: ?? ??? ?file_object.write(reason+'\n')
10-6
try: ? ? a = int(input('Please input first number: ')) ? ? b = int(input('Please input second number: ')) except ValueError: ? ? print('You should input number.') else: ? ? print(a + b)
10-7
while True: ? ? try: ? ? ? ? a = int(input('Please input first number: ')) ? ? except ValueError: ? ? ? ? print('You should input number.') ? ? else: ? ? ? ? break while True: ? ? try: ? ? ? ? b = int(input('Please input second number: ')) ? ? except ValueError: ? ? ? ? print('You should input number.') ? ? else: ? ? ? ? break print(a + b)
10-8
filename='cats.txt' with open(filename) as f: ?? ?contents=f.read() ?? ?print(contents)
filename='dogs.txt' try: ?? ?with open(filename) as f: ?? ??? ?contents=f.read() ?? ??? ? except FileNotFoundError: ?? ?print("没找到。")
else: ?? ?print(contents)
10-9
filename='dogs.txt' try: ?? ?with open(filename) as f: ?? ??? ?contents=f.read() ?? ??? ? except FileNotFoundError: ?? ?pass
else: ?? ?print(contents)
10-10
filename='earth_song.txt' with open(filename) as f: ?? ?contents=f.read() ?? ?i=contents.lower().count("what ") ?? ?print(i)
def count_word(filename): ?? ?with open(filename) as f: ?? ??? ?contents=f.read() ?? ??? ?i=contents.lower().count("the") ?? ??? ?print(f"{filename}有{i}个'the'。") filenames=['earth_song.txt','billie_jean.txt'] for filename in filenames: ?? ?count_word(filename)
10-11
(10-11-1.py)
import json number=input("What is your favorite number?") filename ='number.json' with open(filename,'w') as f: ?? ?json.dump(number,f) ?? ?print("Alright!")
(10-11-2.py)
import json filename='number.json' with open(filename) as f: ?? ?number=json.load(f) ?? ?print(f"I know your favorite number!It's {number}!")
10-12
import json filename='number.json' try: ?? ?with open(filename) as f: ?? ??? ?number=json.load(f) except FileNotFoundError: ?? ?number=input("What is your favorite number?") ?? ?with open(filename,'w') as f: ?? ??? ?json.dump(number,f) ?? ??? ?print("Alright!") else: ?? ?print(f"I know your favorite number!It's {number}!")
10-13
import json
def get_stored_username(): ?? ?filename='username.json' ?? ?try: ?? ??? ?with open(filename) as f: ?? ??? ??? ?username=json.load(f) ?? ?except FileNotFoundError: ?? ??? ?return None ?? ?else: ?? ??? ?return username
def get_new_username(): ?? ?username=input("What is your name?") ?? ?filename='username.json' ?? ?with open(filename,'w') as f: ?? ??? ?json.dump(username,f) ?? ?return username
def greet_user(): ?? ?username=get_stored_username() ?? ?if username: ?? ??? ?message=input("Is this your username?"+username+"(Please enter 'yes'/'no'.)") ?? ??? ?if message=="yes": ?? ??? ??? ?print(f"Welcome back, {username}!") ?? ??? ?else: ?? ??? ??? ?username=get_new_username() ?? ??? ??? ?print(f"We'll remember you when you come back, {username}!") ?? ?else: ?? ??? ?username=get_new_username() ?? ??? ?print(f"We'll remember you when you come back, {username}!") ?? ??? ? greet_user()
|