5.21 - 一入Python深似海,從此節操是路人

來源:互聯網
上載者:User

標籤:sdi   定義   使用   default   內容   報錯   登入   tin   port   

5.21,Python第二節,伊始,我感覺我來到了蜀道的山腳下,抬頭仰望,儘是巍峨,心底又不要臉的有一點兒小期待[*臉紅*]

這節課,我回憶起來都是些零零碎碎,講了字串、列表、字典、切片、檔案等等等等等等等等等等等等等等等等,等到了地老天荒,俯瞰不消化的大肚皮,依稀泛起一股有容乃大的蒼涼。。

不過我覺得每節的作業都是精華,解題的過程就像探險一樣有趣,嘻嘻嘻,所以我想分享出來,留待以後忘光的時候還有地方可以重溫下當年的輝煌:

1、寫一個可以添加商品、查詢商品的程式:
    #1、首先需要登入,登入的使用者名稱和密碼是存在一個檔案裡面的,格式是niuhanyang,123456
    #帳號密碼是輸入正確就登入,
    #登入之後,提示有3個選擇,1、添加商品2、查詢商品3、退出程式,exit()
    #添加商品,需要輸入、商品名稱、商品id、商品價格,都不可為空,價格和id只能是數字,商品名稱不能重複
    #添加商品成功後,要把商品寫入到product.txt這個檔案裡面

  #查詢商品,輸入商品名稱,不能輸入空,如果商品存在,列印商品的資訊
    #商品名:xx,商品價格:xxx,商品id:xxxx
    #如果商品不存在,提示不存在,如果直接輸入exit,那就直接退出程式

name_dict = {}
with open(‘file.txt‘,‘r‘) as fn:
    for line in fn:
        name,passwd = line.strip(‘\n‘).split(‘,‘) # 把line以逗號分隔,去掉分行符號,並且分別定義為name、passwd
        name_dict[name] = passwd # 轉換為有唯一索引的字典,這句的意思是name是key,passwd是value

id = 0
p_dict = {}
while True:
    name = input(‘請輸入使用者名稱:‘).strip()
    if name in name_dict:
        passwd = input(‘請輸入密碼:‘).strip()
        if passwd == name_dict[name]:
            print(‘歡迎%s來到小商品程式,請選擇操作程式:‘ % name)
            print(‘1-添加商品‘.center(50, ‘*‘))
            print(‘2-查詢商品‘.center(50, ‘*‘))
            print(‘3-退出程式‘.center(50, ‘*‘))
            while True:
                action = input(‘請輸入操作程式序號:‘).strip()
                if action == ‘1‘:
                    pname = input(‘請輸入商品名稱:‘).strip()
                    id = id + 1
                    price = input(‘請輸入商品價格:‘).strip()
                    if pname == ‘‘ or price == ‘‘:
                        print(‘商品名稱或價格不可為空!‘)
                    else:
                        if pname in p_dict:
                            print(‘該商品已存在,不能重複添加!‘)
                        else:
                            if price.isdigit() != True:
                                print(‘商品價格必須為數字!‘)
                            else:
                                p_dict.setdefault(pname, {})[‘id‘] = id
                                p_dict.setdefault(pname, {})[‘price‘] = price
                                print(p_dict)
                                with open(‘product.txt‘, ‘w‘) as fp:
                                    product = str(p_dict)
                                    fp.write(product + ‘\n‘)
                                    print(‘添加商品%s成功!‘ % pname)
                elif action == ‘2‘:
                    with open(‘product.txt‘, ‘r‘) as fp:
                        p_list = fp.read()
                        print(p_list)
                    pname = input(‘請輸入商品名稱進行查詢:‘).strip()
                    if pname == ‘‘:
                        print(‘商品名稱不可為空!‘)
                    else:
                        if pname not in p_list:
                            print(‘該商品不存在!‘)
                        else:
                            p_dict = eval(p_list) # 這句的意思是把字串轉換為字典,注意:必須是符合字典格式的字串才能轉換,亂套的7788你是想幹啥
                            print(‘商品名稱:【%s】,商品價格:【%s】,商品ID:【%s】‘ % (pname, p_dict.get(pname).get(‘price‘), p_dict.get(pname).get(‘id‘)))
                elif action == ‘3‘ or action == ‘exit‘:
                    print(‘您已退出程式,謝謝使用。‘)
                    exit()
                    break
                else:
                    print(‘請輸入正確的操作序號!‘)
        else:
            print(‘使用者名稱或密碼錯誤!‘)
    else:
        print(‘該使用者不存在!‘)
    continue

 

另外還有修改檔案的迷之邏輯:

# #下面這些是修改檔案內容的
# import os
# f = open(‘a.txt‘,‘a+‘,encoding=‘utf-8‘)
# # f代表的是這個檔案的對象,也叫控制代碼
# f.seek(0)#移動檔案指標
# fw = open(‘a.txt.new‘,‘w‘,encoding=‘utf-8‘)
# for line in f:
#     new_res = line.replace(‘1‘,‘2‘)
#     fw.write(new_res)
# f.close()
# fw.close()
# os.remove(‘a.txt‘)
# os.rename(‘a.txt.new‘,‘a.txt‘)

我想知道opena.txt.new的時候,真的不會報錯麼。。

剛問了老師,老師說了,不會報錯,因為a+檔案不存在時,是不會報錯的,同時,new出來的這個檔案的命名也是可以很隨意的

 

5.21 - 一入Python深似海,從此節操是路人

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.