Summary of getting started with Python - silent word program

Table of contents

Preface

1. Main functions

2. Usage steps

1. Import the library

2. Code snippet

Summarize


Preface

I was halfway through learning Python and wanted to summarize it, so I tried to write a program for silent words.





1. Main functions

The main function is to ask you to write the corresponding translation of the specified Chinese according to the input string. During this period, you will be prompted for the next step. It will judge whether the format you input meets the requirements of the program. It will disrupt the order of the words you input and put them in the program. Certain situations ask you if you want to exit. Note: I have only learned the first half of the introductory step, so I can only run it in the development tools.




2. Usage steps




1. Import the library

Only touched the random library

import random

2. Code snippet

import random  # 导入random库


# 1.生成列表
def sclist():  # 变量定义
    while True:  # 死循环
        list1 = []  # 定义空序列
        m = int(input("请选择默写的个数:"))  # int(),input()方法
        n = 1
        # while循环
        while n <= m:
            a = input(f"请输入第{n}个单词(格式:英文+空格+中文):")  # 格式化输出
            a = a.strip()  # 去除字符串首尾空格的strip()方法
            # if选择结构
            if a.find(" ") == -1:  # 查找指定字符串的find()方法
                print("找不到空格,请重新输入")
                continue
            elif a.count(" ") != 1:  # 指定子字符串个数的count()方法
                print("应该只保留一个空格,请重新输入")
                continue
            else:
                list1.append(a.lower())  # 追加数据的append()方法
                # print(list1)
            n += 1

        # for循环,遍历分割各个字符串
        for i in range(0, len(list1)):  # range()方法、len()方法
            list1[i] = list1[i].split(" ")  # 分割字符串的split()方法

        # 遍历输出默写的内容,用于确认
        print("您将默写的内容如下:")
        for i in list1:
            print(i)

        YN = input("确定吗?(Y/N)")
        if YN.upper() == "Y":  # 转换大写的upper()方法
            break  # 终止循环的break
        else:
            YN2 = input("退出吗?(Y/N)")
            if YN2.lower() == "y":  # 转换小写的lower()方法
                list1 = 1
                break
    return list1


# 2.主程序
list1 = sclist()  # 调用变量
# list1 = [['access', 'v.进入'], ['configure', 'v.配置']]
while list1 != [] and list1 != 1:

    # 3.随机效果

    list2 = []  # 生成与list1长度相同的空列表
    for i in range(0,len(list1)):
         list2.append([])
    # 打乱顺序
    for i in list1:
        x = random.randint(0,len(list1)-1)  # 生成随机数字
        while list2[x] != []:
            x = random.randint(0, len(list1) - 1)
        list2[x] = i
    # 将得到的数据赋值给list1
    list1 = list2
    # 去除列表中的空列表
    for i in range(len(list2)-1, 0, -1):  # 不知道删除空列表后会不会影响它的长度,所以用与他相同的list2定位
        # 当i指向的数据为空列表时,删除该数据
        if list1[i] == []:
            del list1[i]  # 删除序列数据的del()方法


    # 4.默写阶段
    # 将空字符串赋值给list2,用于接收默写错误的单词
    list2 = []
    i = 0
    while i < len(list1):
        # 输出汉语,让你输入你记忆中的英文翻译
        a = input(f"{list1[i][1]}: ")
        # 当输入退出时,退出这一次的默写
        if a == "退出":
            break
        # 判断您默写的是否与之前输入的是否相等
        elif a != list1[i][0]:
            # 将错误的单词添加到list2用于后面的默写
            list2.append(list1[i])
            # 打印正确的单词,退回重新输入,用于加深印象
            print(list1[i][0])
            i -= 1
        i += 1
    # 将错误的单词打印出来
    for i in list2:
        print(i)
    # 将错误的单词赋值给list1用于下一轮默写
    list1 = list2




Summarize


The learned selection structures, loop structures, strings, lists, some commonly used methods, and preliminary understanding of libraries and variables were used.

Next, continue learning the dictionary. . .

Guess you like

Origin blog.csdn.net/qq_51943845/article/details/121728068