Python 教程2 编辑器及编写简单应用

版权声明:转载请注明出处来源 https://blog.csdn.net/qq_21153225/article/details/84315115

本文介绍简单的Python的开发编辑器及简单应用,代码部分可以直接复制运行即可

Pycharm使用方法

下载pycharm:http://www.jetbrains.com/pycharm/

破解Professional请自行百度,初学者学习使用Community即可

下载后安装即可

pycharm设置

修改设置的字体,File - Setting - Editor - Font中,修改字体为Console

修改默认快捷键 setting - keymap

因人而异,本人经常使用Visual Studio进行开发,所以设置成与VS相同的快捷键,如没有特殊需要不需要修改

几个常用默认的快捷键

Ctrl+Shift+F10 运行当前的页面

Ctrl + / 注释(取消注释)选择的行

Ctrl + D 复制当前行、或者选择的块

Shift + Enter 开始新行

Ctrl+Shift+F 高级查找

Alt + Enter 快速修正
Ctrl + Alt + L 代码格式化

简单使用

在编辑器左边Project面板,新建xxx.py文件,在新出现的页面中输入

print("hello world")

编辑器右上角,点击运行按钮    下方出现运行结果

编辑器右上角,点击结束按钮  ,结束运行

练习:创建一个python文件,复制下列代码,运行;运行后,输入你想查询的城市天气(汉字,国内城市),回车查看结果

import urllib.request

import tkinter
import json
import gzip


cityname = input('你想查询的城市\n')

url = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(cityname)

weather_data = urllib.request.urlopen(url).read()
weather_data = gzip.decompress(weather_data).decode('utf-8')
weather_dict = json.loads(weather_data)
print(weather_dict)
if weather_dict.get('desc') == 'invilad-citykey':
    print(weather_dict)
elif weather_dict.get('desc') == 'OK':
    forecast = weather_dict.get('data').get('forecast')
    startoday = '城市:'+weather_dict.get('data').get('city') +'\n' \
                + '日期:' + forecast[0].get('date') + '\n' \
                + '温度:' + weather_dict.get('data').get('wendu') + '℃\n' \
                + '高温:' + forecast[0].get('high') + '℃\n' \
                + '低温: ' + forecast[0].get('low') + '℃\n' \
                + '风向:' + forecast[0].get('fengxiang') + '\n' \
                + '风力:' + forecast[0].get('fengli') + '\n' \
                + '天气:' + forecast[0].get('type') + '\n' \
                + '感冒:' + weather_dict.get('data').get('ganmao') + '\n'

    one_day = '日期:' + forecast[1].get('date') + '\n' \
              + '天气:' + forecast[1].get('type') + '\n' \
              + '高温:' + forecast[1].get('high') + '\n' \
              + '低温:' + forecast[1].get('low') + '\n' \
              + '风向:' + forecast[1].get('fengxiang') + '\n' \
              + '风力:' + forecast[1].get('fengli') + '\n'

    two_day = '日期:' + forecast[2].get('date') + '\n' \
              + '天气:' + forecast[2].get('type') + '\n' \
              + '高温:' + forecast[2].get('high') + '\n' \
              + '低温:' + forecast[2].get('low') + '\n' \
              + '风向:' + forecast[2].get('fengxiang') + '\n' \
              + '风力:' + forecast[2].get('fengli') + '\n'

    three_day = '日期:' + forecast[3].get('date') + '\n' \
                + '天气:' + forecast[3].get('type') + '\n' \
                + '高温:' + forecast[3].get('high') + '\n' \
                + '低温:' + forecast[3].get('low') + '\n' \
                + '风向:' + forecast[3].get('fengxiang') + '\n' \
                + '风力:' + forecast[3].get('fengli') + '\n'

    four_day = '日期:' + forecast[4].get('date') + '\n' \
               + '天气:' + forecast[4].get('type') + '\n' \
               + '高温:' + forecast[4].get('high') + '\n' \
               + '低温:' + forecast[4].get('low') + '\n' \
               + '风向:' + forecast[4].get('fengxiang') + '\n' \
               + '风力:' + forecast[4].get('fengli') + '\n'
print(one_day)
print(two_day)
print(three_day)
print(four_day)

结果如下:

下一篇:

猜你喜欢

转载自blog.csdn.net/qq_21153225/article/details/84315115