1 九九乘法表 2 登錄程式 3,購物車程式 ...
1 九九乘法表
for i in range(9):#從0迴圈到8 i += 1#等價於 i = i+1 for j in range(i):#從0迴圈到i j += 1 print(j,'*',i,'=',i*j,end = ' ',sep='') # end預設在結尾輸出換行,將它改成空格 sep 預設 j,'*',i,'=',i*j 各元素輸出中間會有空格 print()#這裡作用是輸出換行符
i = 1 while i <= 9: j = 1 while j <= i: print("%d*%d=%d" % (j,i,i*j),end=' ') # 格式化輸出 j += 1 i += 1 print()
2 登錄程式
登錄程式,有三次輸入賬號、密碼的機會,錯誤三次賬號鎖定
user = 'hello world' paswd = 123456 username = input("請輸入用戶名:") password = input("請輸入密碼:") for i in range(3): if username == user and int(password) == paswd: #判斷用戶名和密碼是否都匹配 print("歡迎您的到來") break elif i < 2: username = input("請輸入用戶名:") password = input("請輸入密碼") elif i == 2: print("賬戶已鎖定") break
3,購物車程式
功能:
1,輸入餘額
2,顯示商品列表
3,輸入要購買的東西
4,判斷是否有能力購買
5,是否繼續
6,輸出已購買的商品和餘額
shop_car = []#用來存放購買的商品 goods = { 1:['手機',2500], 2:['電腦',3500], 3:['自行車',4500], 4:['寶馬',20000] }#商品列表 while True: salary = input("你有多少錢:") # 輸入有多少錢 if salary.isdigit(): # 判斷是否為整數 salary = int(salary) # 將字元串轉化為整數 print("是否要買東西:") flag1 = input("Y N:") if flag1.upper() == 'N': # 將字元串大寫 exit("歡迎下次光臨") # 退出程式並輸出“歡迎下次光臨” elif flag1.upper() == 'Y': break # 終止迴圈 elif flag1.upper() == 'Q': exit("歡迎下次光臨") elif salary.upper() == 'Q': exit("歡迎下次光臨") while True: print("淘寶".center(30,'-')) # 輸出以-----淘寶------ for i in goods: # 迴圈輸出 print(i,goods[i]) print("淘寶".center(30, '-')) choice_good = input("請輸入商品編碼:")#接受一個字元串 if choice_good.isdigit() : choice_good = int(choice_good) if choice_good >= 1 and choice_good <= 4: if salary >= goods[choice_good][1]: shop_car.append(goods[choice_good][0]) # 給字典中添加元素 salary = salary - goods[choice_good][1] print("您購買的商品為:", goods[choice_good][0]) print("餘額為:", salary) print("是否繼續:") contin = input("Y N") if contin.upper() == 'N': break elif contin.upper() == 'Q': break else: print('餘額不足') print("是否繼續:") contin = input("Y N") if contin.upper() == 'N': break elif contin.upper() == 'Q': break else : print("沒有這個編號") continue # 暫停本次迴圈 elif choice_good.upper() == "Q" : break else: print("我看不懂。。。") print("你買了:",end =' ') for i in shop_car: print(i,end=' ') print() print("餘額為:",salary) print("歡迎下次光臨")