Python编程:从入门到实践的动手试一试答案(第十章)
来源:互联网 发布:按键精灵自动录入数据 编辑:程序博客网 时间:2024/05/21 09:13
#10-1 Python学习笔记with open('learning_python.txt') as file_object: contents = file_object.read() print(contents)----------------------------------------------with open('learning_python.txt') as file_object: for line in file_object: print(line.rstrip())----------------------------------------------with open('learning_python.txt') as file_object: lines = file_object.readlines()for line in lines: print(line.rstrip())
#10-2 C语言学习笔记with open('learning_python.txt') as file_object: lines = file_object.readlines()for line in lines: line1 = line.replace('Python','C') print(line1.rstrip())
#10-3 访客filename = 'guest.txt'with open(filename,'w') as file_object: name = input('请输入你的名字:') file_object.write(name)
#10-4 访客名单filename = 'guest_book.txt'with open(filename,'a') as file_object: print('按‘q’推出') while True: name = input('请输入你的名字:') if name == 'q': break file_object.write(name + '\n')
#10-5 关于编程的调查filename = 'reason.txt'with open(filename,'a') as file_object: print('按‘q’推出') while True: reason = input('请问你为何喜欢编程:') if reason == 'q': break file_object.write(reason + '\n')
#10-6 加法运算try: number1 = int(input('请输入一个数字:')) number2 = int(input('请在输入一个数字:')) result = number1 + number2 print(result)except ValueError: print('你输入的格式有问题')
#10-7 加法计算器while True: try: number1 = int(input('请输入一个数字:')) number2 = int(input('请在输入一个数字:')) result = number1 + number2 print(result) except ValueError: print('你输入的格式有问题')
#10-8 猫和狗try: with open('cats.txt') as cat: print(cat.read()) with open('dogs.txt') as dog: print(dog.read())except FileNotFoundError: print('对不起没有找到文件')
#10-9 沉默的猫和狗try: with open('cats.txt') as cat: print(cat.read()) with open('dogs.txt') as dog: print(dog.read())except FileNotFoundError: pass
#10-10 常见单词try: with open("The Girl Scouts' Canoe Trip by Edith Lavell.txt") as book: content = book.read() number = content.lower().count('the') print(number)except FileNotFoundError: pass
#10-11 喜欢的数字import jsonfilename = 'numbers.json'number = input('请输入你最喜欢的数字:')with open(filename, 'w') as f_obj: json.dump(number, f_obj)with open(filename) as f_obj: number = json.load(f_obj) print('I know your favorite number! It is ___' + number)
#10-12 记住喜欢的数字import jsonfilename = 'numbers.json'try: with open(filename) as f_obj: number = json.load(f_obj) print('I know your favorite number! It is ___' + number)except FileNotFoundError: number = input('请输入你最喜欢的数字:') with open(filename, 'w') as f_obj: json.dump(number, f_obj)
#10-13 验证用户import jsondef get_stored_username(): """如果存储了用户名,就获取它""" filename = 'username.json' try: with open(filename) as f_obj: username = json.load(f_obj) except FileNotFoundError: return None else: return usernamedef 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 usernamedef greet_user(): """问候用户,并指出其名字""" username = get_stored_username() if username: yep = input('Is ' + username + ' your name?(y/n)') 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()
阅读全文
0 0
- Python编程:从入门到实践的动手试一试答案(第十章)
- Python编程:从入门到实践的动手试一试答案(第十章)
- Python编程:从入门到实践的动手试一试答案
- Python编程:从入门到实践的动手试一试答案(第二章)
- Python编程:从入门到实践的动手试一试答案(第三章)
- Python编程:从入门到实践的动手试一试答案(第四章)
- Python编程:从入门到实践的动手试一试答案(第五章)
- Python编程:从入门到实践的动手试一试答案(第六章)
- Python编程:从入门到实践的动手试一试答案(第七章)
- Python编程:从入门到实践的动手试一试答案(第八章)
- Python编程:从入门到实践的动手试一试答案(第九章)
- Python编程:从入门到实践的动手试一试答案(第十一章)
- Python编程:从入门到实践的动手试一试答案(第十一章)
- Python编程:从入门到实践的动手试一试答案(第九章)
- Python编程:从入门到实践的动手试一试答案(第六章)
- Python编程:从入门到实践的动手试一试答案(第七章)
- Python编程:从入门到实践的动手试一试答案(第四章)
- Python编程:从入门到实践的动手试一试答案(第三章)
- APP内支付的接入总结(支付宝&APP STORE)
- log4j从配置文件中读取路径
- Unit4
- MapReduce/Hadoop的TopN解决方案之键唯一的情况
- QT之qss教程-qss文件使用方法
- Python编程:从入门到实践的动手试一试答案(第十章)
- MySQL多线程复制
- Kinect Fusion Explorer D2D C++ Sample中函数简介
- rocketmq 启动
- iOS 开发之键盘类型UIKeyboardType
- callable和future和runnable
- session
- knn(k-NearestNeighbor) with scala
- python基础-闭包、装饰器