python基礎習題練習
阿新 • • 發佈:2017-12-20
python基礎習題練習#encoding:utf-8
#1.實現用戶輸入用戶名和密碼,當用戶名為 seven 且 密碼為 123 時,顯示登陸成功,否則登陸失敗!
name=input('name>>: ').strip()
password=input('passwd>>: ').strip()
if name=='seven' and password=='123':
print('login successful')
else:
print('login failed')
#2.實現用戶輸入用戶名和密碼,當用戶名為 seven 且 密碼為 123 時,顯示登陸成功,否則登陸失敗,失敗時允許重復輸入三次
n=0
while n < 3:
name=input('name>>: ').strip()
password=input('passwd>>: ').strip()
if name=='seven' and password=='123':
print('login successful')
else:
print('login failed')
n+=1
#3.實現用戶輸入用戶名和密碼,當用戶名為 seven 或 alex 且 密碼為 123 時,顯示登陸成功,否則登陸失敗,失敗時允許重復輸入三次
n=0
while n < 3:
name=input('name>>: ').strip()
passwd=input('passwd>>: ').strip()
if name=='seven' or name=='alex' and passwd=='123':
print('login successful')
else:
print('login failed')
n += 1
#8.a 使用while循環實現輸出2-3+4-5+6...+100 的和
n=2
sum=0
while n <= 100:
if n%2==0:
sum+=n
else:
sum-=n
n+=1
print(sum)
#b.使用 while 循環實現輸出 1,2,3,4,5, 7,8,9, 11,12 使用 while 循環實現輸出 1-100 內的所有奇數
encoding:utf-8
n=1
while n <= 12:
if n==6 or n==10:
n+=1
continue
print(n)
n+=1
#b.使用 while 循環實現輸出 1-100 內的所有奇數
n=1
while n<=100:
if n %2 !=0:
print(n)
n+=1
#e.使用 while 循環實現輸出 1-100 內的所有偶數
n=1
while n<=100:
if n%2==0:
print(n)
n+=1
#9現有如下兩個變量,請簡述 n1 和 n2 是什麽關系?
# n1 = 123456
# n2 = n1
# print(id(n1))
# print(id(n2))
#關系:值相同,ID相同
# 2 作業:編寫登陸接口
#
# 基礎需求:
#
# 讓用戶輸入用戶名密碼
# 認證成功後顯示歡迎信息
# 輸錯三次後退出程序
count=0
while count <3:
name=input('name>>: ').strip()
passwd=input('passwd>>: ').strip()
if name=='aa' and passwd=='123':
print('login successful')
else:
print('input error,you have %d choice' %(2-count))
count+=1
# 升級需求:
#
# 可以支持多個用戶登錄 (提示,通過列表存多個賬戶信息)
# 用戶3次認證失敗後,退出程序,再次啟動程序嘗試登錄時,還是鎖定狀態(提示:需把用戶鎖定的狀態存到文件裏)
##(在本級目錄下先創建一個db.txt文件)
dic={
'egon1':{'password':'123','count':0},
'egon2':{'password':'123','count':0},
'egon3':{'password':'123','count':0},
}
count=0
while True:
name=input('name>>: ').strip()
if name not in dic:
print('用戶不存在')
continue
with open('db.txt','r') as f:
lock_users=f.read().split('|')
if name in lock_users:
print('用戶%s被鎖定,'%name)
break
if dic[name]['count'] > 2:
print('次數過多,鎖定')
with open('db.txt','a') as f:
f.write('%s|' %name)
break
passwd=input('passwd>>: ')
if passwd==dic[name]['password']:
print('登陸成功')
break
else:
print('用戶名,密碼錯誤')
dic[name]['count']+=1
python基礎習題練習