您的位置:首页 > 其它

练习-99乘法表 token生成器 翻译小工具

2018-04-18 00:06 204 查看

一、99乘法表

  

import random
li=[x for x in range(5)]
print(li)

#  前面部分不一定要跟后面的x有关系,后面只是控制次数
li=[random.choice('defijjdiw31') for x in range(5)]
print(li)

#因为x,只是用于控制次数,我们可以用 _ 来代替,节省内存空间
count = int(input())
li=[random.choice('defijjdiw31') for _ in range(count)]
print(li)
列表推导式  

2.2 代码

  

#### 第一版本 lowb版本
'''
import random
token = []
count = int(input()) #  输入要生成几位的随机数

for i in range(count):
x = random.choice('asdeadasd1234567')
token.append(x)
token = ''.join(token)
print(token)
'''

#### 第二版本  string 版本

import string, random
token = []
s = string.ascii_letters + string.digits
count = int(input())
for i in range(count):
x = random.choice(s)
token.append(x)
print(''.join(token))

#### 第三版本 列表推导版本

import string, random count = int(input()) s = string.ascii_letters + string.digits ''.join([random.choice(s) for _ in range(count)])   #### 除此之外 还可以 使用char()内置函数,定义ascii中的字母的数值的边界来完成

 三、字典小工具

'''
字典小程序:
1.可以查询单词
2.可以自定义补充单词
3.可以删除某个单词
'''

print('欢迎来到LH的字典王国'.center(30, '-'))

my_dict = {'中文':'Chinese','书':'book','西瓜':'watermelon'}

querry = input('请输入要查询的中文:').strip()
#  查询的中文,去除两边的空格

if my_dict.get(querry):
print(f'你查询的中文为:{querry},意思是:{my_dict[querry]}')
else:
add = input('没有查询到,是否愿意为小词扩产词库(y/n)').strip()
#避免输入的时候,多加了空格,先去除两边的空格
if add == 'y':
print(my_dict)
print('谢谢帮助,请添加单词和相关解释,用冒号分割,')
words = input('实例:(书:book)').strip()
if len(words.split(':')) == 2: #  使用英文冒号分割
words = words.split(':')
my_dict[words[0]] = words[1]
elif len(words.split(':')): #  使用中文冒号分割
words = words.split(':')
my_dict[words[0]] = words[1]
else:
print('输入有错,请按照正确的方式')
print(my_dict)
else:
print('88')

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: