求1-100所有数的和
sum=0 for i in range(0,101) : sum=sum+i print(sum)
输出1-100内的所以奇数
for i in range (1,101,2) : print(i)
输出1-100内所有偶数
for i in range(1,100,2): print(i+1)
求1-2+3-4+5+。。。+99的所有数的和
sum=0 for i in range (1,100,2): sum=sum+i for j in range (2,99,2): sum=sum-j print(sum)
打印字符串对应的索引,利用索引值去获取字符串中的每一个字符
text=input("请输入") for i in range (len(text)) : print(text[i],i)
计算用户输入内容有几个十进制小数?几个字母
count_num=0 count_ch=0 count_space=0 text=input("请输入一串任意字符") for i in text: if i.isdigit(): count_num=count_num+1 elif i.isalpha(): count_ch=count_ch+1 elif i.isspace(): count_space=count_space+1 print("这段文字中共有%d个数字"%count_num) print("这段文字中共有%d个字母"%count_ch) print("这段文字中共有%d个空格"%count_space)
实现一个整数加法计算器
count_num=0 count_ch=0 count_space=0 text=input("请输入一串任意字符") for i in text: if i.isdigit(): count_num=count_num+1 elif i.isalpha(): count_ch=count_ch+1 elif i.isspace(): count_space=count_space+1 print("这段文字中共有%d个数字"%count_num) print("这段文字中共有%d个字母"%count_ch) print("这段文字中共有%d个空格"%count_space)
开发敏感词语过滤程序,提示用户输入内容,如果用户输入的内容包括苍井空,东京热,将内容替换为***
warn=['苍老师','东京热'] text=input("请输入内容") for i in warn: if text == i: text=text.replace(text,"***") print(text)