下面會寫一些基礎的例題,有興趣的自己也可以練練手!
1.假設手機短信收到的數字驗證碼為“278902”,編寫一個程序,讓用戶輸入數字驗證碼,如果數字驗證碼輸入正確,提示“支付成功”;否則提示“數字驗證碼錯誤”
number=int(input('請輸入手機驗證碼:'))
if number==278902:print('支付成功')
else:print('數字驗證碼錯誤')
2.修改為隨機生成一組數字驗證碼。,編寫一個程序,讓用戶輸入數字驗證碼,如果數字驗證碼輸入正確,提示“支付成功”;否則提示“數字驗證碼錯誤”
import random
str1 = ""
for i in range(7):str1 += str(random.randint(0,9))
print(str1)
number=input('請輸入手機驗證碼:')
if number==str1:print('支付成功')
else:print('數字驗證碼錯誤')
3.判斷閏年與平年
year=int(input('請輸入查詢的年份:'))
if (year%4==0 and year%100!=0) or (year%400==0):print(year,'是閏年')
else:print(year,'是平年')
4.假設今天星期4,求第n天之后星期幾,使用鍵盤輸入n值。
weekday = int(input("請輸入星期幾:"))
print("今天星期",weekday,"請問n天之后星期幾:")
n = int(input("請輸入n的值:"))
weekday=(weekday+n)%7
if weekday == 0:weekdayName ="星期日"
else:weekdayName ="星期"+str(weekday)
message ="n天之后星期"+weekdayName
print(message)
5.鍵盤輸入用戶的身高與體重,使用身體質量指數BMI的數值,判斷用戶身體的健康情況。提示:BMI = 體重 / (身高 * 身高)。體重以kg為單位,身高以m為單位.
weight=eval(input("請輸入體重(kg):"))
height=eval(input("請輸入身高(m):"))
BMI=weight/height**2
if BMI<18.5 :message="偏瘦"
elif BMI<25 :message="正常"
elif BMI<30 :message="偏胖"
else :message="肥胖"
print(BMI)
print(message)
6.某城市出租車計費方式為:出租車起步價8元,包含2千米;超過兩千米的部分,每千米收取1.5元;超過12千米的部分,每千米收取2元。編碼實現輸入行駛千米數,計算出需要支付的費用
a=int(input("輸入行駛千米數(km):"))
if a<=2 :b=8
elif a<=12 :b=8+(a-2)*1.5
else :b=8+10*1.5+(a-12)*2
message="需要支付的費用:"+str(b)+"元"
print(message)
7.?象限是平面直角坐標系中橫軸和縱軸所劃分的四個區域,每一個區域叫做一個象限。象限以原點為中心,x和y軸為分界線。右上的稱為第一象限(x>0,y>0),左上的稱為第二象限(x<0,y>0)…。請輸入坐標值,判斷用戶輸入的坐標屬于第幾象限。
x=eval(input("請輸入坐標x的值:"))
y=eval(input("請輸入坐標y的值:"))
if x>0 :if y>0 :message="此坐標在第一象限"else :message="此坐標在第四象限"
else :if y<0 :message="此坐標在第三象限"else :message="此坐標在第二象限"
print(message)
8.求從1970年到2100年的閏年有
message="從1970年到2100年的閏年有:\n"
count=0
for year in range(1970,2100):if (year%4==0)and(year&100!=0)or(year%400==0):message+=str(year)count+=1if count%5==0:message+="\n"else:message+="\t"
print(message)
9.打印乘法口訣表
message=""
for i in range(1,10):for j in range(1,i+1):multi=str(j)+"*"+str(i)+"="+str(j*i)message+=multi+"\t"message+="\n"
print(message)