Python樣例匯編
(1)乘法表
# -*- coding: UTF-8 -*-
?
def main():
??? #********begin********
??? for i in range(1,10):
??????? for j in range (i,10):
??????????? print("{} x {} = {}".format(i,j,i*j),end=" ")
??????? print()
??? #********end********
main()
?
?
'''
def main():
??? #********begin********
??? for i in range(1,10):
??????? for j in range (i,10):
??????????? print("{} x {} = {}".format(i,j,i*j),end=" ")
??????? print()
??? #********end********
main()
'''
(2)素?cái)?shù)
# -*- coding: UTF-8 -*-
# 輸出指定范圍內(nèi)的素?cái)?shù)
# take input from the user
lower = int(input("輸入?yún)^(qū)間最小值: "))
upper = int(input("輸入?yún)^(qū)間最大值: "))
#**********begin1************
______ num in range(lower,upper + 1):
#**********end1************
??????? # 素?cái)?shù)大于 1
??????? if num > 1:
??????????????? #**********begin2************
??????????????? for i in ______(2,num):
??????????????? #**********end2************
??????????????????????? if (num % i) == 0:
??????????????????????????????? break
??????????????? #**********begin3************
??????????????? ______
????????????? ??#**********end3************
??????????????????????? print(num)
'''
# take input from the user
lower = int(input("輸入?yún)^(qū)間最小值: "))
upper = int(input("輸入?yún)^(qū)間最大值: "))
#**********begin1************
for num in range(lower,upper + 1):
#**********end1************
??????? # 素?cái)?shù)大于 1
??????? if num > 1:
??????????????? #**********begin2************
??????????????? for i in range(2,num):
??????????????? #**********end2************
??????????????????????? if (num % i) == 0:
??????????????????????????????? break
????????? ??????#**********begin3************
??????????????? else:
??????????????? #**********end3************
??????????????????????? print(num)
'''
(3)溫度轉(zhuǎn)換
TempStr=input("請(qǐng)輸入帶有符號(hào)的華氏溫度值(例如:135F):")
while TempStr[-1] not in ['N','n']:
??? #**********begin1**********
??? if ________ in ['F','f']:
??? #**********end1**********
??????? #**********begin2**********
??????? C=(eval(________) -32)/1.8
??????? #**********end2**********
??????? #**********begin3**********
??????? print("轉(zhuǎn)換后的攝氏溫度是________C".format(C))
??????? #**********end3**********
??????? break
??? else:
??????? print("輸入格式錯(cuò)誤!")
??????? break
?
'''
TempStr=input("請(qǐng)輸入帶有符號(hào)的華氏溫度值(例如:135F):")
while TempStr[-1] not in ['N','n']:
??? #**********begin1**********
??? if TempStr[-1] in ['F','f']:
??? #**********end1**********
??????? #**********begin2**********
??????? C=(eval(TempStr[0:-1]) -32)/1.8
??????? #**********end2**********
??????? #**********begin3**********
??????? print("轉(zhuǎn)換后的攝氏溫度是{:.2f}C".format(C))
??????? #**********end3**********
??????? break
??? else:
??????? print("輸入格式錯(cuò)誤!")
??????? break
'''
(4)至少輸入一個(gè)三位數(shù)
x = input('輸入一個(gè)至少三位的數(shù)字:')
try:
??? x = int(x)
??? #**********begin1**********
??? x = x________100
??? #**********end1**********
??? #**********begin2**********
??? if x________0:
??? #**********end2**********
??????? print('輸入至少三位的整數(shù)!')
??? #**********begin3**********
??? ________
??? #**********end3**********
??????? print(x)
?
except BaseException:
??? print('必須輸入整數(shù)!')
???
'''
x = input('輸入一個(gè)至少三位的數(shù)字:')
try:
??? x = int(x)
? ??#**********begin1**********
??? x = x//100
??? #**********end1**********
??? #**********begin2**********
??? if x==0:
??? #**********end2**********
??????? print('輸入至少三位的整數(shù)!')
??? #**********begin3**********
??? else:
??? #**********end3**********
??????? print(x)
?
except BaseException:
??? print('必須輸入整數(shù)!')
'''
(5)求和
print('請(qǐng)輸入五個(gè)數(shù)字:')
#**********begin1**********
v=[]
#**********end1**********
?
i=0
s=0
#**********begin2**********
________ i<5:
#**********end2**********???
?
??? a=input('n%d=' % (i+1))???????
??? #**********begin3**********
??? v.________([int(a)])
??? #**********end3**********
??? s=s+v[i]
??? i+=1
print("參數(shù)顯示列表:{}".format(v))
print('sum=%d' % s)
?
'''
print('請(qǐng)輸入五個(gè)數(shù)字:')
#**********begin1**********
v=[]#定義一個(gè)空列表
#**********end1**********
?
i=0
s=0
#**********begin2**********
while i<5:
#**********end2**********???
?
??? a=input('n%d=' % (i+1))???????
??? #**********begin3**********
??? v.extend([int(a)]) #接收列表,并把參數(shù)列表中的元素添加到列表尾部。
??? #**********end3**********
??? s=s+v[i]
??? i+=1
print("參數(shù)顯示列表:{}".format(v))
print('sum=%d' % s)
?
'''
(6)畫(huà)圓
import turtle
def main(a):
???????
??????? turtle.penup()
??????? #**********begin1**********
??????? turtle.________(0,a)
??????? #**********end1**********
??????? #**********begin2**********
??????? turtle.________()
??????? #**********end2**********
??????? #**********begin3**********
??????? turtle.________(-a)
??????? #**********end3**********
?
main(80)
?
'''
goto
pendown
circle
'''
(7)存款
cunkuan = 10000
years=0
?
#**********begin1**********
while cunkuan________20000:
#**********end1**********
???
??? #**********begin2**********
??? years=________
??? #**********end2**********
??? cunkuan=cunkuan*(1+0.0325)
print(str(years)+"年以后,存款會(huì)翻番")
#**********begin3**********
________
#**********end3**********
'''
< #存款小于20000才循環(huán)
years+1 #循環(huán)一次加一年
pyinstaller? #第三方庫(kù),python源文件打包
'''
(8)乒乓球隊(duì)
for i in range(ord('x'),ord('z') + 1):
???? for j in range(ord('x'),ord('z') + 1):
???????????? #**********begin1**********
???????????? ________ i!=j:
???????????? #**********end1**********
????????????????????
???????????????????? #**********begin2**********
???????????????????? ________ k in range(ord('x'),ord('z') + 1):
???????????????????? #**********end2**********
???????????????????????????? if (i != k) and (j != k):
???????????????????????????????????? if (i != ord('x')) and (k != ord('x')) and (k != ord('z')):
???????????????????????????????????????????? print('order is a -- %s\t b -- %s\t c--%s' % (chr(i),chr(j),chr(k)))
'''
if
for
'''