Python随笔1-练习题

来源:互联网 发布:软件蓝图设计 编辑:程序博客网 时间:2024/06/15 03:24

Python随笔1-练习题

1、求1-100的所有数的和

sum =0
for i inrange(101):
    sum = sum + i
print(sum)

 

2、输出 1-100 内的所有奇数

方法一:

fori inrange(100):
    ifi%2!= 0:
        print(i)

方法二:

fori inrange(1,100,2):
    print(i)

 

 

3、输出 1-100 内的所有偶数

方法一:

fori inrange(100):
    ifi%2== 0:
        print(i)
方法二:
for i inrange(0,100,2):
    print(i)

 

 

4、用户登陆(三次机会重试)

 

#!/usr/bin/env python
#-*- coding:utf-8 -*-
importsys
retry_counter =0
whileretry_counter <3:
    name = input("请输入你的用户名:").strip()
    f = open('lock.txt')
    fori inf.readlines():
        i = i.strip().split()
        ifname ini[0]:
            print('这个账户:%s 已经被锁住了 ' % name)
            sys.exit()
    f = open('account.txt')
    passwd = input('请输入你的密码:').strip()
    match_flag = False
    for
line inf.readlines():
        username,password = line.strip().split()
        ifusername == name and passwd == password:
            print('欢迎 %s 进入系统' % name)
            match_flag = True
            break
    if
match_flag == False:
        print('用户名或者密码匹配,请重新输入')
        retry_counter +=1
        f =open('lock.txt','r+')
        f.write('\n'+ name)
        f.close()
else:
    print('你输入的次数太多了,账号%s已经被锁定'% name)

 

 

 

 

1 0
原创粉丝点击