程序代码

CheckWeather1.py:

#coding=utf-8

import platform,codecs

print ('Python',platform.python_version())

#定义天气字典

city_weather={}

#将TXT文件内容读取到字典

f = codecs.open( 'weather_info.txt', 'r', 'utf-8' )

for lines in f.readlines():

line=lines.strip().split(',')

city_weather[line[0]] = line [1]

f.close()

#历史查询记录

history={}

#不断循环

while True:

user_input=input('\n请输入指令或您要查询的城市名:')

#将输入转换为小写

user_input=user_input.lower()

if user_input=="quit" or user_input=="q":

print ('感谢您的使用,再见!')

exit(0)

elif user_input=="help" or user_input=="h" or user_input=="?":

print('''

- 输入城市名,获取该城市的天气情况;

- 输入 help,获取本帮助信息;

- 输入 history,获取历史查询信息;

- 输入 quit,退出天气查询系统。''')

elif user_input=="history":

#增加首次未查询成功时的提示

if history:

for h in history:

print (h,history[h])

else:

print ('并无查询历史记录')

elif user_input in city_weather:

print (user_input, city_weather[user_input])

history[user_input]=city_weather[user_input]

else:

print ('''你输入的指令错误或者城市名称不存在,请重新输入。

- 如果不清楚指令,可以输入 help,获取帮助。

''')

程序输出

G:\Python\Py103\Chap1\project>py -3 CheckWeather1.py

Python 3.6.0

请输入指令或您要查询的城市名:history

并无查询历史记录

请输入指令或您要查询的城市名:上海

上海 小雨

请输入指令或您要查询的城市名:北京

北京 晴

请输入指令或您要查询的城市名:纽约

你输入的指令错误或者城市名称不存在,请重新输入。

- 如果不清楚指令,可以输入 help,获取帮助。

请输入指令或您要查询的城市名:history

上海 小雨

北京 晴

请输入指令或您要查询的城市名:help

- 输入城市名,获取该城市的天气情况;

- 输入 help,获取本帮助信息;

- 输入 history,获取历史查询信息;

- 输入 quit,退出天气查询系统。

请输入指令或您要查询的城市名:quit

感谢您的使用,再见!

G:\Python\Py103\Chap1\project>

results matching ""

    No results matching ""