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 if i % 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)