python第三周小测

1.读取一个文件,显示除了井号(#)开头的行意外的所有行

# -*- coding: utf-8 -*-
"""
Created on Tue May 28 09:37:08 2019

@author: Omega_Sendoh
"""
#打开文件
f = open("install-sh","r")
#读取文件的所有行,以列表形式存储,每行为列表中的一个字符串元素
res = f.readlines()
#循环整个列表,去除以空格开头的行的空格,然后去除以#号开头的行的#号
for i in res:
    if i[0] == "#":
        continue
    else:
        print(i)

2.无重复字符的最长子串

# -*- coding: utf-8 -*-
"""
Created on Tue May 28 10:57:55 2019

@author: Omega_Sendoh
"""

"""
定义一个空的字符串,从起始位置开始搜索输入的字符串,如果字符没有出现在wind中,则把字符加入wind
如果字符出现在wind中,则找到字符串中出现该相同字符的位置,删除该重复字符之前的所有字符
并重新加入该字符
""" 
def LongestString(s):
    
    wind = ''
    l=0
    
    for i in s:
         if i not in wind:
            wind +=i
            l=max(l,len(wind))
         else:
            wind = wind[wind.index(i)+1:] + i 
    return l

s=input('enter string:')
print(LongestString(s))

3.制作一个密码簿,其可以存储一个网址,和一个密码,请编写程序完成这个密码簿的增删改查功能,并且实现文件存储功能。

import json
    
def add_info():
    #打开存储文件,判断文件中是否有内容
    with open('usr.json','a+') as f:
         info = f.read()
    #如果没有内容,创建一个字典,以字典的方式存储网址与密码
    if not info:
         with open('usr.json','a+') as f:
             full_info = {}
             net_add = input('enter your URL:')
             passwd = input('enter your password:')
             full_info[net_add] = passwd
             print('add success')
             json.dump(full_info,f)
    #若文件中有内容,则把文件中的内容转换为python的字典类型
    else:
         with open('usr.json','r') as f :
             full_info = json.load(f)
             #print((full_info))
             net_add = input('enter your URL:')
             passwd = input('enter your password:')
             full_info.setdefault(net_add,passwd)
             print('add success')
         #给字典中添加对象,再次写入文件中(即添加新的信息后重新更新文件的内容)    
         with open('usr.json','w') as f :
             json.dump(full_info,f)

def del_info():
     with open('usr.json','r') as f:
          full_info = json.load(f)
          #输入想要删除的网址
          net_add = input('enter your delete URL:')
          #如果该网址存在,则删除网址与密码,把更改后的数据存到文件中
          if net_add in full_info:
              del full_info[net_add]
              print('delete success')
              with open('usr.json','w') as f:    
                   json.dump(full_info,f)
         #若该网址不存在,提示网址不存在
          else:
              print('This URL not exist')

def change_info():
    with open('usr.json','r') as f:
          full_info = json.load(f)
          #输入要更改的网址与密码
          net_add = input('enter your change URL:')
          passwd = input('enter your new password:')
          if net_add in full_info:
              full_info[net_add] = passwd
              print('change password succcess')
              with open('usr.json','w') as f:    
                   json.dump(full_info,f)
          else:
              print('This URL not exist')
    
def check_info():
    with open('usr.json','r') as f:
         full_info = json.load(f)
         net_add = input('enter your check URL:') 
         if net_add in full_info:
            print('This URL password  is:',full_info[net_add])
         else:
             print('This URL not exist')
             
print('you can choose: add/del/change/check/quit')
while 1:
     obj = input('enter your choose:')
     if obj == 'add':
          add_info()  
     elif obj == 'del':
          del_info()
     elif obj == 'change':
          change_info()
     elif obj == 'check':
          check_info()
     else:
          break

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值