Python基礎-小程式練習,python基礎小程式

來源:互聯網
上載者:User

Python基礎-小程式練習,python基礎小程式

一、 從第3層迴圈直接跳出所有迴圈

break_flag = Falsecount = 0while break_flag == False:    print("-第一層")    while break_flag == False:        print("第二層")        while break_flag == False:            count += 1            if count > 10:                break_flag = True            print("第三層")print("keep going...")
with break_flag

 

 

# break_flag = Falsecount = 0while count < 3:    print("-第一層")    while count < 3:        print("第二層")        while count <= 10:            count += 1            # if count > 10:            #     break_flag = True            print("第三層")print("keep going...")
without break_flag

 

 

二、 購物車程式

goods_list = [['Iphone7',5800],['Coffee',30],['Book',99],['Bike',199],['Vivi X9',2499]]     #商品列表shopping_cart = []      #使用者購物車列表salary = int(input('input your salary:'))       #使用者薪水m = salaryk = 0while True:    index = 0    for goods in goods_list:        #列印商品列表        print(index,goods)        index += 1    choice = input('>>>:').strip()    if choice.isdigit():        choice = int(choice)        if choice >= 0 and choice < len(goods_list):            goods = goods_list[choice]            if goods[1] <= salary:      #判斷使用者是否帶了足夠的錢來支付所選商品                shopping_cart.append(goods)                salary -= goods[1]                print('Add goods '+str(goods[0])+' into shopping cart! Your current balance:'+str(salary))            else:                print('No enough money!The price is:'+str(goods[1])+'! Need more:'+str(goods[1]-salary))        else:            print('Have no this goods!')    elif choice == 'q':        print('----------Your shopping cart----------')        print('ID   goods   quantity    price   total')        for i in range(len(goods_list)):            j = shopping_cart.count(goods_list[i])            if j > 0 :                k += 1                print(k,'\t',goods_list[i][0],'\t',j,'\t\t',goods_list[i][1],'\t',j * goods_list[i][1])        print('Total price is:',m - salary)        break    else:        print('Have no this goods')
購物車程式

goods_list = [['Iphone7',5800],['Coffee',30],['Book',99],['Bike',199],['Vivi X9',2499]]  # 商品列表shopping_cart = {}  # 使用者購物車列表從列表變為字典salary = int(input('input your salary:'))  # 使用者薪水# m = salary #不需要此項# k = 0 #使用者購物車商品列印ID,移動位置至列印迴圈外while True:    index = 0    for goods in goods_list:  # 列印商品列表        print(index, goods)        index += 1    choice = input('>>>:').strip()    if choice.isdigit():  # 判斷是否為數字        choice = int(choice)        if choice >= 0 and choice < len(goods_list):  # 商品存在            goods = goods_list[choice]            if goods[1] <= salary:  # 判斷使用者是否帶了足夠的錢來支付所選商品                if goods[0] in shopping_cart:  # 之前買過                    shopping_cart[goods[0]][1] += 1  # 購物數量加1                else:                    shopping_cart[goods[0]] = [goods[1], 1]  # 建立一條新增商品的購買記錄                salary -= goods[1]  # 扣錢                print('Add goods ' + str(goods[0]) + ' into shopping cart! Your current balance:' + str(salary))            else:                print('No enough money!The price is:' + str(goods[1]) + '! Need more:' + str(goods[1] - salary))        else:            print('Have no this goods!')    elif choice == 'q':        id_counter = 1  # 初始化商品ID        total_cost = 0  # 初始化商品總花費        print('----------Your shopping cart----------')        print('ID   goods     quantity    price     total')        for i in shopping_cart:            print("%-5s%-10s%-12s%-10s%-s"                  % (                      id_counter, i, shopping_cart[i][1], shopping_cart[i][0],                      shopping_cart[i][0] * shopping_cart[i][1]))            id_counter += 1  # 商品ID自加1            total_cost += shopping_cart[i][0] * shopping_cart[i][1]  # 使用者已購商品總花費        print('Total price is:', total_cost)        break    else:        print('Have no this goods')
購物車程式最佳化,使用字典

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.