練習一
阿新 • • 發佈:2018-07-29
進行 += 開頭 賬戶 pass 實現 start art lis
1、使用range打印100,99,98,....1,0
1 for i in range(100,-1,-1): 2 print(i)
2、用while和for分別計算出1-2+3-4....+99
1 num = 1 2 num2=0 3 while num < 100: 4 if num % 2 == 0: 5 num2=num2-num 6 else: 7 num2=num2+num 8 num+=1 9 print(num2) 10 11 sum = 0 12 for i in range(1,100): 13 ifi % 2 == 0: 14 sum = sum - i 15 else: 16 sum= sum + i 17 print(sum)
3、計算用戶輸出中索引為奇數並且對應元素為數字的個數.
1 num = input("輸入內容") 2 d = 0 3 for i in num: 4 if num.find(i)%2!=0: 5 if i.isdigit(): 6 d+=1 7 print(d)
4、移除列表中li的空格,將以A和a開頭或者c結尾的的元素添加到新的列表,打印這個列表
1 li=["abcd","xiaoming","Actv "," dabai","tuzi"," aabc","AAbb"] 2 num=[] 3 for i in li: 4 i=i.strip() 5 if i.startswith("a") or i.startswith("A") or i.endswith("c"): 6 num.append(i) 7 for v in num: 8 print(v)
5、實現一個整數加法計算器:
如:content = input(‘請輸入內容:’) # 如用戶輸入:5+8+7....(最少輸入兩個數相加),然後進行分割再進行計算,將最後的計算結果添加到此字典中(替換None):
dic={‘最終計算結果’:None}
content = input("請輸入內容") content_list= content.split("+") dic={"結果":None} num=0 for i in content_list: i=i.strip() num=num+int(i) dic["結果"]=num print(dic)
6、模擬某公司錄入賬戶和密碼的程序
1:賬戶密碼儲存到 tx_list= [{"賬戶":xxx,"密碼":xxx},{"賬戶":xxx,"密碼":xxx)]
2:檢查非法字符:board = ["huang","du"]
3:輸入賬戶和密碼時,可以持續輸入。q可退出,如果非法字符,將非法字符替換成同數量的**添加到tx_list中,每次輸入後打印出賬戶和密碼
1 tx_list= [] 2 board = ["huang","du"] 3 while 1 : 4 username = input("輸入用戶名(退出請輸入Q)") 5 if username.upper() == "Q": 6 exit(print(tx_list)) 7 password = input("輸入密碼") 8 for i in board: 9 if i in username: 10 username=username.replace(i,"*"*len(i)) 11 tx_list.append({"賬戶" :username,"密碼":password}) 12 print("賬戶:",username,"密碼:",password)
練習一