python exercise1

来源:互联网 发布:html5手机端模板 知乎 编辑:程序博客网 时间:2024/06/15 05:41

1.

#!/usr/bin/env Python
# coding:utf-8
_author_ = "tom"

'''
@author:houruiyun
@file:2.py
@contact:674211605.qq.com
@time:6/25/172:45 AM
@desc:
'''
for i in range(1,4):
    name = raw_input('please input your name:')
    password = raw_input('your password:')
    if  (name == 'root' and password == 'westos'):
        print 'welcome to user manager system'
        exit (0)
    else:
        print 'the user and password is not matching'
print ('你已经登陆过三次,请十分钟后登陆')



2.打印所有小于10并且大于或等于0的所有偶数; 
打印所有小于10并且大于或等于0的最大偶数 
这里写图片描述

源代码:

# /usr/bin/env python# coding:utf-8_author_ = "lvah"'''@author:fairy@file:homework1.py@time:17-7-5下午11:32@DESC;'''print range(0,10,2)print range(1,10,2)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3.判断一个数是否为质数 
源代码:

#/usr/bin/env python#coding:utf-8_author_ = "lvah"'''@author:fairy@file:3.py@time:17-7-6上午1:43@DESC;'''num = input('请输入数字:')if num > 1:    for n in range(2,num):        if ( num % n ) == 0:            print num,'不是质数'            break    else:        print num,'是质数'else:    print num,'不是质数'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

这里写图片描述

测试: 
这里写图片描述

这里写图片描述

这里写图片描述

8.创建一用户登陆程序:保存用户的用户名与密码 
新建用户时,判断用户名是否已经存在,若已经存在则报错; 
登陆系统时,确保用户名存在于系统中,密码正确,有三次机会,超过三次则报错 
源代码:

# /usr/bin/env python# coding:utf-8_author_ = "lvah"'''@author:fairy@file:8.py@DESC;'''Username = []Password = []print '''    登陆(L)ogin   新建(N)ew'''def login():    if Username == []:        print  '无用户,请新建用户'    else:        count = 0        while count<3:            name_in = raw_input('name:')            pass_in = raw_input('password:')            if name_in in Username:                i = Username.index(name_in)                pass1 = Password[i]                if pass_in == pass1:                    print 'welcome to user system'                    exit(0)                else:                    count += 1            else:                count += 1        print 'error'def new():    name2 = raw_input('new name:')    if name2 not in Username:        Username.append(name2)        pass2 = raw_input('new password:')        Password.append(pass2)        print 'Adding new user successful'    else:        print 'error : the user has exists'def main():    cho = 'Enter your choice:'    while True:        pro = raw_input(cho).lower()        if pro == 'l':            login()        elif pro == 'n':            new()        else:            print 'you can input l,n'main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

测试: 
这里写图片描述