Python | 翻译小程序

1、回顾上章内容

1.1、使用random+string自动生成密码

#!/usr/bin/env python
# Author:ywyankerp
import random
import string
My_List = []
My_Name = input("请您设置您的用户名:")
My_Password = input("请您选择设置几位数的密码:")
for i in range(int(My_Password)):
    My_Ran = random.choice(string.ascii_uppercase + string.digits)
    My_List.append(My_Ran)
Resultss = "".join(My_List)
User =  f"您的用户名为:{My_Name}  您的密码为:{Resultss}"
print(User)
>>>请您设置您的用户名:延凯
>>>请您选择设置几位数的密码:9
您的用户名为:延凯  您的密码为:LZT1W3MMR

2、本章知识点

  • dict
  • set
  • tuple

2、字典的使用

2.1、字典结构 找到一个对应关系 {“键” : “值”} {“1” : “2”} dict 集合

#!/usr/bin/env python
# Author:ywyankerp
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
print(My_Name_dict)
>>>{'延凯': 'yankai', '张三': 'zhangsan', '王五': 'wangwu'}

2.2、取值

#!/usr/bin/env python
# Author:ywyankerp
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Name = My_Name_dict["延凯"]
print(My_Name)
>>>yankai

例如:我们取一个不存在的值

#!/usr/bin/env python
# Author:ywyankerp
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Name = My_Name_dict["uwaydoawo"]
print(My_Name)
>>>Traceback (most recent call last):
  File "e:\Python_yanzan\Python-yankai\My_dict.py", line 8, in <module>
    My_Name = My_Name_dict["uwaydoawo"]
KeyError: 'uwaydoawo'

查询没有的值将会直接报错

2.3、使用.get方式取值

#!/usr/bin/env python
# Author:ywyankerp
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Name = My_Name_dict.get("延凯")
print(My_Name)
>>>yankai # 输出结果

那么使用.get 方式取一个没有的值效果如下:

#!/usr/bin/env python
# Author:ywyankerp
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Name = My_Name_dict.get("pwadjdpjwap;dwa")
print(My_Name)
>>>None # 返回结果为空! 不会提示错误信息

例如:使用.get方式取一个值,如果没有这个值讲输出一条话 如下:

#!/usr/bin/env python
# Author:ywyankerp
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Name = My_Name_dict.get("pwadjdpjwap;dwa","对不起 您查找的内容无翻译")
print(My_Name)
>>>对不起 您查找的内容无翻译 # 输出此段话

My_Name = My_Name_dict.get(“pwadjdpjwap;dwa”,"对不起 您查找的内容

解释:如果"pwadjdpjwap;dwa"没有内容 那么,“输出后面这段话!”

2.4、在字典中添加键值

#!/usr/bin/env python
# Author:ywyankerp
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Name = My_Name_dict
print(My_Name)
>>>{'延凯': 'yankai', '张三': 'zhangsan', '王五': 'wangwu'}
# 目前字典中有yankai zhangsan wangwu值

直接添加键值如下:

#!/usr/bin/env python
# Author:ywyankerp
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_ADD = My_Name_dict["年龄"] = "27"
# 字典名称["添加键"] = "值"
print(My_Name_dict)
>>>{'延凯': 'yankai', '张三': 'zhangsan', '王五': 'wangwu', '年龄': '27'} # 输出结果

2.5、在字典中可直接添加列表list

#!/usr/bin/env python
# Author:ywyankerp
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_ADD = My_Name_dict["列表"] = [1, 2, 3, 4, 5, 6,]
print(My_Name_dict)
>>>{'延凯': 'yankai', '张三': 'zhangsan', '王五': 'wangwu', '列表': [1, 2, 3, 4, 5, 6]} # 输出结果

同时我们可以先定义列表随后嵌套到字典中如下:

#!/usr/bin/env python
# Author:ywyankerp
My_List = ["周一", "周二", "周三", "周四", "周五", ] # 先定义一个列表
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Add = My_Name_dict["我的列表"] = My_List # 加入列表内容到字典
print(My_Name_dict)
>>>{'延凯': 'yankai', '张三': 'zhangsan', '王五': 'wangwu', '我的列表': ['周一', '周二', '周三', '周四', '周五']} # 输出内容

同时我们也可以嵌套字典如下:

#!/usr/bin/env python
# Author:ywyankerp
My_List = ["周一", "周二", "周三", "周四", "周五", ]
Add_book_dict = {
    "book" : "python3",
    "price" : 100
}
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Add =  My_Name_dict["我的书"] = Add_book_dict
print(My_Name_dict)
>>> {'延凯': 'yankai', '张三': 'zhangsan', '王五': 'wangwu', '我的书': {'book': 'python3', 'price': 100}} # 输出结果

删除字典的某一个键

#!/usr/bin/env python
# Author:ywyankerp
My_List = ["周一", "周二", "周三", "周四", "周五", ]
Add_book_dict = {
    "book" : "python3",
    "price" : 100
}
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Add =  My_Name_dict["我的书"] = Add_book_dict
del My_Name_dict["王五"] # 例如:删除王五的键
print(My_Name_dict)
>>> {'延凯': 'yankai', '张三': 'zhangsan', '我的书': {'book': 'python3', 'price': 100}} # 王五已经不存在

删除 del 字典名称[“键”] 即可删除

修改字典中键的值

#!/usr/bin/env python
# Author:ywyankerp
My_List = ["周一", "周二", "周三", "周四", "周五", ]
Add_book_dict = {
    "book" : "python3",
    "price" : 100
}
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Add =  My_Name_dict["我的书"] = Add_book_dict
My_Name_dict["延凯"] = "yankerp" # 修改键值
print(My_Name_dict)
>>> {'延凯': 'yankerp', '张三': 'zhangsan', '王五': 'wangwu', '我的书': {'book': 'python3', 'price': 100}} # 输出结果

修改: 字典名称[“键”] = “值”

3、使用keys方法获取字典中的所以key

#!/usr/bin/env python
# Author:ywyankerp
My_List = ["周一", "周二", "周三", "周四", "周五", ]
Add_book_dict = {
    "book" : "python3",
    "price" : 100
}
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Add =  My_Name_dict["我的书"] = Add_book_dict
My_keys = My_Name_dict.keys() # 使用keys方法
print(My_keys)
>>> dict_keys(['延凯', '张三', '王五', '我的书']) # 输出内容

使用for循环输出

#!/usr/bin/env python
# Author:ywyankerp
My_List = ["周一", "周二", "周三", "周四", "周五", ]
Add_book_dict = {
    "book" : "python3",
    "price" : 100
}
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Add =  My_Name_dict["我的书"] = Add_book_dict
for i in My_Name_dict.keys():
    print(i)
>>> # 以下是输出结果
>延凯
张三
王五
我的

使用items方法

一个方法 # 键值对 成对 可以理解为元组 一组一组的。

#!/usr/bin/env python
# Author:ywyankerp
My_List = ["周一", "周二", "周三", "周四", "周五", ]
Add_book_dict = {
    "book" : "python3",
    "price" : 100
}
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Add =  My_Name_dict["我的书"] = Add_book_dict
My_itm = My_Name_dict.items()
print(My_itm)
>>> dict_items([('延凯', 'yankai'), ('张三', 'zhangsan'), ('王五', 'wangwu'), ('我的书', {'book': 'python3', 'price': 100})]) # 输出结果内容 可以理解为一组一组的

使用for循环输出items

#!/usr/bin/env python
# Author:ywyankerp
My_List = ["周一", "周二", "周三", "周四", "周五", ]
Add_book_dict = {
    "book" : "python3",
    "price" : 100
}
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Add =  My_Name_dict["我的书"] = Add_book_dict
for i in My_Name_dict.items():
    print(i)
('延凯', 'yankai')
('张三', 'zhangsan')
('王五', 'wangwu')
('我的书', {'book': 'python3', 'price': 100})

那么以上输出已经可以看到是一组一组的输出 我们可以把键&&值分别设定变量输出 如下:

#!/usr/bin/env python
# Author:ywyankerp
My_List = ["周一", "周二", "周三", "周四", "周五", ]
Add_book_dict = {
    "book" : "python3",
    "price" : 100
}
My_Name_dict = {
    "延凯" : "yankai",
    "张三" : "zhangsan",
    "王五" : "wangwu"
}
My_Add =  My_Name_dict["我的书"] = Add_book_dict
for a, b in My_Name_dict.items():
    My_fules = f"{a}=={b}"
    print(My_fules)
# 输出结果:
延凯==yankai
张三==zhangsan
王五==wangwu
我的书=={'book': 'python3', 'price': 100}

3、tuple元组

name = 1,2
print(name)
>>(1,2)

加括号

name = (1,2)
print(name)
>>(1,2)

3.1、例如IP地址及端口

Ip_Port = ("192.168.1.100","80")
print(Ip_Port)
>>('192.168.1.100', '80')

3.2、将Ip_port元组嵌套字典

My_IP = {} # 定义一个空的字典
Ip_Port = ("192.168.1.100","80") # 定义一个元组
My_IP["My_ip_port"] = Ip_Port # 在字典中加入元组内容
print(My_IP) # 输出字典
>>>{'My_ip_port': ('192.168.1.100', '80')} # 输出内容

不重复的元素

set 集合
nuber = {1,2,2,3,3,4,5,6,7,7,7,7,78,8,9,}
set
nuber
>>{1, 2, 3, 4, 5, 6, 7, 8, 9, 78} 
for i in nuber:
    print(i)
1
2
3
4
5
6
7
8
9
78
name = {1,2,3,4,4,4,4,4,5,5,5,5,6,7,8,9,0}
result = set(name)
print(result)
>>>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
type(result)
set

4、编写翻译小程序

#!/usr/bin/env python
# -*- coding: utf-8 -*-
print("欢迎来到yankerp的翻译系统,要登录翻译系统请先注册.".center(50, "-"))
import random
import string
import sys
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("退出翻译系统成功,欢迎下次光临!!!")

运行结果如下:

----------欢迎来到yankerp的翻译系统,要登录翻译系统请先注册.-----------
请您设置您的用户名:延凯
请选择您的密码为几位数:9
您的用户名密码设置成功 用户名:延凯 您的密码为:n9eiu9xe4
-----------恭喜您注册用户名密码成功,请输入用户名密码登录翻译系统------------
请您输入您的用户名:延凯
请您输入您的密码:n9eiu9xe4
用户名密码输入正确
--------------恭喜您成功登陆翻译系统,请您慢慢享用~~~---------------
请您输入你需要翻译:蟒蛇
您要查询的结果为:蟒蛇 翻译结果为:Python

输入用户名密码如下:

----------欢迎来到yankerp的翻译系统,要登录翻译系统请先注册.-----------
请您设置您的用户名:延凯
请选择您的密码为几位数:9
您的用户名密码设置成功 用户名:延凯 您的密码为:mm9yg5czo
-----------恭喜您注册用户名密码成功,请输入用户名密码登录翻译系统------------
请您输入您的用户名:延凯
请您输入您的密码:a
请您检查用户名密码是否正确,请核对后再次输入!!

添加翻译key如下:

----------欢迎来到yankerp的翻译系统,要登录翻译系统请先注册.-----------
请您设置您的用户名:延凯
请选择您的密码为几位数:9
您的用户名密码设置成功 用户名:延凯 您的密码为:er5zfn0iw
-----------恭喜您注册用户名密码成功,请输入用户名密码登录翻译系统------------
请您输入您的用户名:延凯
请您输入您的密码:er5zfn0iw
用户名密码输入正确
--------------恭喜您成功登陆翻译系统,请您慢慢享用~~~---------------
请您输入你需要翻译:a
对不起您所需要翻译的 'a' 未找到!!!
您是否要添加翻译信息(y/n?)y
请您输入你想添加的Key:张三
请您输入你想添加的value:zhangsan
恭喜您添加成功-翻译名为:张三 翻译值为:zhangsan
中国 == China
延凯 == YanKai
周一 == Monday
蟒蛇 == Python
运动 == sport
钢 == steel
驾驶 == steer
张三 == zhangsa

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