zoukankan      html  css  js  c++  java
  • 编程小白的第一本Python入门书

    例1:实现一个清除敏感字并写入文件的功能

    #创建文件并写入
    def text_create(name,msg):
        desktop_path = 'c:\Users\lenovo\Desktop\'
        full_path = desktop_path + name + '.txt'
        file = open(full_path,'w')
        file.write(msg)
        file.close()
        print('done')
    
    #清除敏感字
    def text_filter(word,censored_word='lame',changed_word='Awesome'):
        return word.replace(censored_word,changed_word)
    
    #main函数
    def censored_text_create(name,msg):
        clean_msg = text_filter(msg)
        text_create(name,clean_msg)
    
    censored_text_create('Try','lame!lame!lame!') #censored:受审查的,被删剪的

    例2:实现一个登录密码功能

    #登录
    def account_login():
        password = input('password:')
        if password == '12345':
            print('Login success!')
        else:
            print('Wrong password or invalid input!') #Wrong:错误的,不正确的   invalid:无效的,不能成立的
         account_login()
    account_login()

    例3:改进例2,增加重置密码功能

    #登录带重置密码功能
    password_list = ['*#*#','12345']
    def account_login():
        password = input('Password:')
        password_correct = password == password_list[-1] #correct 正确的,合适的
        password_reset = password == password_list[0]    
        if password_correct:
            print('Login success!')
        elif password_reset:
            new_password = input('Enter a new password:')
            password_list.append(new_password)
            print('Your password has changed successfully!')
            account_login()
        else:
            print('Wrong password or invalid input!')
            account_login()
    account_login()

    例4:改进例3,增加密码错误3次锁定功能

    #登录带重置密码功能
    password_list = ['*#*#','12345']
    def account_login():
        tries = 3
        while tries > 0: #while-else结构
            password = input('Password:')
            password_correct = password == password_list[-1]
            password_reset = password == password_list[0]
            if password_correct:
                print('Login success!')
            elif password_reset:
                new_password = input('Enter a new password:')
                password_list.append(new_password)
                print('Your password has changed successfully!')
                account_login()
            else:
                print('Wrong password or invalid input!')
                tries = tries - 1
                print(tries,'times left')
        else:
            print('Your account has been suspended') #suspended:暂停
    account_login()

    例5: 统计文本中的词频

    import string
    
    path = 'Walden.txt'
    
    with open(path, 'r') as text:
        words = [raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()]
        # print(string.punctuation) !"#$%&'()*+,-./:;<=>?@[]^_`{|}~
        words_index = set(words)
        counts_dict = {index:words.count(index) for index in words_index}
    
        for word in sorted(counts_dict,key=lambda x:counts_dict[x],reverse=True):
            print('%s-%s times' %(word, counts_dict[word]))
  • 相关阅读:
    计算素数。
    两个div在一行中显示
    html文本太长显示为省略号的方法
    .net remoting实例
    C#核心基础--静态类&部分类
    C#核心基础--类的继承
    c#核心基础--类的构造方法
    C#核心基础--类的声明
    C#核心基础--浅谈类和对象的概念
    c#学习之旅------01
  • 原文地址:https://www.cnblogs.com/jp-mao/p/6634137.html
Copyright © 2011-2022 走看看