python 學習 迴文數 、 田字格 、猜數遊戲、統計不同字元個數DAY15
迴文數
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))