Python基础笔记(1)

Python基础实验功能记录

import os
import random
import re
import dic as dic


# 题目一
def fun_login(really_password):
    mpassword = really_password
    mlogintime = 3
    while mlogintime != 0:
        ampassword = input()
        if ampassword.__eq__(mpassword):
            print("Login success!")
            mlogintime = 3
            break
        else:
            mlogintime -= 1
            if mlogintime == 0:
                print("Your account has been suspended")
                break
            else:
                print("Wrong password or invalid input,you still have " + str(mlogintime) + " chances")


# 题目二
def fun_texcreat(name, text):
    t = open(name + '.txt', 'a')
    t.write(text)
    t.close()
    text = text.replace("暴力", '**')
    t = open('last.txt', 'a')
    t.write(text)
    t.close()


# 题目三
def roll_dice():
    data = [random.randint(1, 6), random.randint(1, 6), random.randint(1, 6)]
    return data


def roll_result(data):
    ans = 0
    for i in data:
        ans += i
    return ans


def start_game():
    data = roll_dice()
    ans = roll_result(data)
    ain = input()
    if ain.__eq__("Big") and ans > 11:
        print("YEAS")
    elif ain.__eq__("Big"):
        print("NO")
    elif ain.__eq__("Small") and ans <= 11:
        print("YEAS")
    else:
        print("NO")


# 题目四
def readfile():
    txt = open('Z:\\大学相关\\课程\\大三\\Python大数据处理\\实验一\\Walden.txt').read()
    txt = txt.lower()
    r = '[’!"#$%&\'()*+,./:;<=>?@[\\]^`{|}~]+'
    txt = re.sub(r, ' ', txt)
    txt = txt.split(' ')
    txt2 = set(txt)

    fdata = {}
    for i in txt2:
        fdata[i] = txt.count(i)
    fdata = sorted(fdata.items(), key=lambda x: x[1], reverse=True)
    print(fdata)


if __name__ == "__main__":
    # fun_login("pass")
    # fun_texcreat("old_data", "碗底暴力毫得暴力缩进相同的一组语句baoli构成一个代码块,我们称之代码组。像if、while、def和class这样的复合语句,首行以关键字开始,以冒号( : )结束,该行之后的一行或多行代码构成代码组。我们将首行及后面的代码组称为一个子句(clause)。")
    # start_game()
    readfile()

 

你可能感兴趣的:(Python)