Python基础笔记(1)

本文介绍了Python编程的基础实验,包括登录验证、文件操作、骰子游戏和文本分析等功能实现。通过具体代码示例,展示了如何使用Python进行基本的编程任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值