zoukankan      html  css  js  c++  java
  • 练习-99乘法表 token生成器 翻译小工具

    一、99乘法表

      

     1.1 技术点

      记住:

      for 循环的使用,以及for的嵌套使用

      range()的使用,掌握sep为负数的使用的使用。

      print() 函数的使用,默认的结尾的换行符 替换 end= ' '

      f-format的使用,控制字符的宽度 {x:4}  这样x的输出即使4个字符的

    1.2 代码部分

    版本1:数字版本 使用f-format的宽度来控制格式化对齐
    for i in range(9, 0, -1): for j in range(1,i+1): print(f'{j}*{i}={i*j:2}', end=' ') print() 版本2:使用字符串的方式来控制格式化对齐 for i in range(9, 0, -1): for j in range(1, i+1): print(f'{j}*{i}={str(i*j).ljust(2)}', end=' ') print() 版本3:中文版本的数字,就是用到列表索引的转换 ch_num = ['零','一','二','三','四','五','六','七','八','九'] # print(ch_num[0]) for i in range(9, 0, -1): for j in range(1, i+1): result = str(i*j).zfill(2) # 用字符0去填充 left = ch_num[int(result[0])] right = ch_num[int(result[1])] if int(result) > 9: print(f'{ch_num[j]}*{ch_num[i]}={left}十{right}', end=' ') else: print(f'{ch_num[j]}*{ch_num[i]}={right:5}', end=' ') print()

    二、token生成器

    2.1 技术点解决 

    • random 标准库随机取数值

        使用random.choice方法(另外有一个choices使用方法比较复杂就不使用那个了)

          ranmdom.choice(seq) 从一个序列中返回一个元素

    • string 标准库东西

        是一个字符串常亮的集合。可以用于枚举acsII码的字符,这样我们就不用手写一大堆集合了。

        help查看里面的数据

          

    • 字符串与数字的这练习 join方法
    >>> s = ['a','b','c','d','e']
    >>> ''.join(s)
    'abcde'
    >>> s = ['a','b','c','d','e',1,2]
    >>> ''.join(s)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: sequence item 5: expected str instance, int found
    >>> s = ['a','b','c','d','e','1','2']
    >>> ''.join(s)
    'abcde12'
    join方法练习
    • 列表与列表推导式

         append 向一个列表中添加元素

         列表推导式:

     1 import random
     2 li=[x for x in range(5)]
     3 print(li)
     4 
     5 #  前面部分不一定要跟后面的x有关系,后面只是控制次数
     6 li=[random.choice('defijjdiw31') for x in range(5)]
     7 print(li)
     8 
     9 #因为x,只是用于控制次数,我们可以用 _ 来代替,节省内存空间
    10 count = int(input())
    11 li=[random.choice('defijjdiw31') for _ in range(count)]
    12 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')
           
  • 相关阅读:
    温故而知新 js 点击空白处关闭气泡
    javascript 打印错误信息 catch err
    ajax application/json 的坑
    nodejs 的好基友:pm2
    有道翻译 / 百度翻译Api
    PHP 正则表达式
    php 正则替换
    github get 请求指定页面的代码
    H5 input 聚焦 置顶
    autoHotKey 一些脚本积累
  • 原文地址:https://www.cnblogs.com/louhui/p/8871008.html
Copyright © 2011-2022 走看看