马士兵Python学习笔记_P146_145.实操案例一
一、实操案例一
二、代码示例-向文件输出“奋斗成就更好的你”
# 1、向文件输出“奋斗成就更好的你”
# 方式一:使用print方式进行输出(输出的目的地是文件)
fp = open("P146_145_test1.txt", "w", encoding="utf-8")
print("奋斗成就更好的你", file=fp)
fp.close()
# 方式二:使用文件读写操作
with open("P146_145_test2.txt", "w", encoding="utf-8") as wfile:
wfile.write("奋斗成就更好的你")
运行结果
D:\Environment\Python\Python311\python.exe D:\Environment\PythonWorks\learnpython\马士兵Python\第17章_实操案例\P146_145.实操案例一.py
Process finished with exit code 0
三、代码示例-输出北京天气预报
# 2、输出北京天气预报
print("星期日", "今天") # 个数可变的位置参数
print("----------------------------------")
print("08时", "11时", "14时", "17时", "20时", "23时")
print("0℃ ", "6℃ ", "10℃ ", "4℃ ", "1℃ ", "0℃")
print("----------------------------------")
print("明天 ", "2/23 ", "2℃/11℃")
print("星期二 ", "2/24 ", "0℃/9℃")
print("星期三 ", "2/25 ", "-2℃/8℃")
print("星期四 ", "2/26 ", "-3℃/6℃")
print("星期五 ", "2/27 ", "-2℃/7℃")
print("星期六 ", "2/28 ", "-1℃/11℃")
运行结果
D:\Environment\Python\Python311\python.exe D:\Environment\PythonWorks\learnpython\马士兵Python\第17章_实操案例\P146_145.实操案例一.py
星期日 今天
----------------------------------
08时 11时 14时 17时 20时 23时
0℃ 6℃ 10℃ 4℃ 1℃ 0℃
----------------------------------
明天 2/23 2℃/11℃
星期二 2/24 0℃/9℃
星期三 2/25 -2℃/8℃
星期四 2/26 -3℃/6℃
星期五 2/27 -2℃/7℃
星期六 2/28 -1℃/11℃
Process finished with exit code 0