1.判斷一個數是否爲質數
python
#!/usr/bin/env pythonapp
#coding:utf-8ide
__author__ = "lvah"測試
'''spa
@author:linblog
@file:zhishu.pyutf-8
@contact:xc_123456789@qq.comget
@time:7/7/178:41 AMinput
@descit
'''
num=input('請輸入數字:\n')
if num > 0:
for m in range(2,num-1):
if num%m==0:
print "不是質數"
break
else:
print "是質數"
else:
print "輸入的數字有誤"
測試:
2.建立一用戶登陸程序:保存用戶的用戶名與密碼
新建用戶時,判斷用戶名是否存在,若已經存在則報錯
登陸系統時,確保用戶名存在於系統中,密碼正確,有三次機會,超過三次報錯
程序以下:
#!/usr/bin/env python
#coding:utf-8
__author__ = "lvah"
'''
@author:linwei
@file:denglu.py
@contact:xc_123456789@qq.com
@time:7/7/1711:28 AM
@desc
'''
users=['1']
passwd=['1']
print '''
welcome to login system
'''
def create():
user1=raw_input("請輸入建立的用戶名:\n")
if user1 not in users:
users.append(user1)
pass1=raw_input("請輸入給新用戶設置的密碼:\n")
passwd.append(pass1)
print "建立用戶成功"
else:
print"用戶已經存在,能夠進行登錄"
def login():
b=0
while b<3:
user2=raw_input('請輸入須要登錄的用戶名:\n')
pass2=raw_input('請輸入與用戶相匹配的密碼:\n')
if user2 in users:
a=users.index(user2)
pass4=passwd[a]
if pass4==pass2:
print "登錄成功"
exit(0)
else:
b=b+1
else:
b=b+1
print"輸入的密碼錯誤"
def main():
while True:
user3=raw_input('請輸入用戶名:\n')
if user3 not in users:
print "用戶不存在,需創建"
create()
login()
else:
create()
login()
break
main()
測試:
判斷用戶存在的狀況,首先在users和passwd中加上用戶名與密碼
測試以下: