回文数
getnum = input("请输入一个自然数:")
if getnum == getnum[::-1]:
print("{0}是回文数".format(getnum))
else:
print("该数不是回文数")
田字格1
for i in range(13):
if i in [0,6,12]:
print("+ - - - + - - - +")
else:
print("1 1 1")
田字格2
def line1(n):
if n in [0,5,10,15,20]:
print("+ - - - "*4+'+')
else:
print("1 "*4+'+')
for i in range(21):
line1(i)
猜数字
count = 0
n = 6
while True:
getnum = eval(input("请输入不大于10的整数:"))
if getnum < 6 :
print("遗憾,太小了!")
count += 1
elif getnum > 6:
print("遗憾,太大了!")
count +=1
else:
print("预测{0}次,你猜中了!".format(count))
break
统计不同字符
count1,count2,count3,count4 = 0,0,0,0
getNum = input("请输入一行字符:")
for i in getNum:
if ord('0') <= ord(i) <= ord('9'):
count1 += 1
elif ord('a') <= ord(i) <= ord('z') or ord('A') <= ord(i) <= ord('Z'):
count2 += 1
elif ord(' ') == ord(i):
count3 += 1
else:
count4 += 1
print("数字、英文、空格、其它字符个数分别为:{0} {1} {2} {3}".format(count1,count2,count3,count4))