Python | 编写一个简单的多功能程序

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import string
import sys
import urllib.request
import json
import gzip

print("欢迎来到Yankerp系统" .center(50, "-"))
My_menu = {
    "1" : "货币转换",
    "2" : "翻译系统",
    "3" : "天气预报"
}
for a, b in My_menu.items():
    print(f"{a}、{b}")
Your_list_Choice = input("请您选择您需要的服务:")
if Your_list_Choice == "1":
    print("欢迎来到货币转换系统" .center(50, "-"))
    print("此系统目前只支持人民币转美元,欢迎使用~~~")
    your_money = input("请您输入你需要转的金额:")
    your_money = int(your_money)
    My_conversion = (f"您需要转换的人民币为{your_money}元 转换为美元结果为:{(your_money) / 6.6}$")
    print(My_conversion)
elif Your_list_Choice == "2":
    print("欢迎来到yankerp的翻译系统,要登录翻译系统请先注册.".center(50, "-"))
    Your_name = input("请您设置您的用户名:")
    Your_Password = input("请选择您的密码为几位数:")
    My_list = []
    for i in range(int(Your_Password)):
        My_Password = random.choice(string.ascii_lowercase + string.digits)
        My_list.append(My_Password)
        My_List_Com = "".join(My_list)
    print(f"您的用户名密码设置成功 用户名:{Your_name} 您的密码为:{My_List_Com}")
    print("恭喜您注册用户名密码成功,请输入用户名密码登录翻译系统".center(50, "-"))
    Your_Sign = input("请您输入您的用户名:")
    Your_Sign_pass = input ("请您输入您的密码:")
    if Your_Sign == Your_name and Your_Sign_pass == My_List_Com:
        print("用户名密码输入正确")
    else:
        print("请您检查用户名密码是否正确,请核对后再次输入!!!")
        sys.exit()
    print("恭喜您成功登陆翻译系统,请您慢慢享用~~~".center(50, "-"))
    My_dict = {
        "中国" : "China",
        "延凯" : "YanKai",
        "周一" : "Monday",
        "蟒蛇" : "Python",
        "运动" : "sport",
        "钢" : "steel",
        "驾驶" : "steer",
    }
    Your_query = input("请您输入你需要翻译:")
    if (My_dict.get(Your_query, '')):
        print(f"您要查询的结果为:{Your_query} 翻译结果为:{My_dict[Your_query]}")
    else: 
        print(f"对不起您所需要翻译的 '{Your_query}' 未找到!!!")
        Your_Add = input("您是否要添加翻译信息(y/n?)")
        if Your_Add == "y":
            Key = input("请您输入你想添加的Key:")
            value = input("请您输入你想添加的value:")
            Add_dict = My_dict[Key] = value
            print(f"恭喜您添加成功-翻译名为:{Key} 翻译值为:{value}")
            for a, b in My_dict.items():
                print(f"{a} == {b}")
        else:
            print("退出翻译系统成功,欢迎下次光临!!!")
if Your_list_Choice == "3":
    cityname = input('你想查询的城市?\n')

#访问的url,其中urllib.parse.quote是将城市名转换为url的组件
url = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(cityname)
#发出请求并读取到weather_data
weather_data = urllib.request.urlopen(url).read()
#以utf-8的编码方式解压数据
weather_data = gzip.decompress(weather_data).decode('utf-8')
#将json数据转化为dict数据
weather_dict = json.loads(weather_data)
print(weather_dict)
if weather_dict.get('desc') == 'invilad-citykey':
    print("输入的城市名有误")
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)

------------------欢迎来到Yankerp系统-------------------
1、货币转换
2、翻译系统
3、天气预报
请您选择您需要的服务:1
--------------------欢迎来到货币转换系统--------------------
此系统目前只支持人民币转美元,欢迎使用~~~
请您输入你需要转的金额:9
您需要转换的人民币为9元 转换为美元结果为:1.3636363636363638$

------------------欢迎来到Yankerp系统-------------------
1、货币转换
2、翻译系统
3、天气预报
请您选择您需要的服务:3
你想查询的城市?
北京

日期:9日星期一
天气:雷阵雨
高温:高温 30℃
低温:低温 22℃
风向:东南风
风力:<![CDATA[<3]]>

日期:10日星期二
天气:多云
高温:高温 29℃
低温:低温 22℃
风向:东风
风力:<![CDATA[<3]]>

日期:11日星期三
天气:雷阵雨
高温:高温 25℃
低温:低温 22℃
风向:东南风
风力:<![CDATA[<3]]>

日期:12日星期四
天气:阴
高温:高温 28℃
低温:低温 23℃
风向:西南风

------------------欢迎来到Yankerp系统-------------------
1、货币转换
2、翻译系统
3、天气预报
请您选择您需要的服务:2
----------欢迎来到yankerp的翻译系统,要登录翻译系统请先注册.-----------
请您设置您的用户名:延凯
请选择您的密码为几位数:9
您的用户名密码设置成功 用户名:延凯 您的密码为:xp0ncxv2j
-----------恭喜您注册用户名密码成功,请输入用户名密码登录翻译系统------------
请您输入您的用户名:延凯
请您输入您的密码:xp0ncxv2j
用户名密码输入正确
--------------恭喜您成功登陆翻译系统,请您慢慢享用~~~---------------
请您输入你需要翻译:中国
您要查询的结果为:中国 翻译结果为:China

你可能感兴趣的:(Python,Python全栈开发之路)