python資料類型練習題

來源:互聯網
上載者:User

標籤:else   log   資料類型   pen   inpu   根據   span   購物車   end   

一、元素分類

有如下值集合 [11,22,33,44,55,66,77,88,99,90...],將所有大於 66 的值儲存至字典的第一個key中,將小於 66 的值儲存至第二個key的值中。
即: {‘k1‘: 大於66的所有值, ‘k2‘: 小於66的所有值}

li=[11,22,33,44,55,66,77,88,99,90]dic={‘k1‘:[ ],‘k2‘:[ ]}for i in li:    print(i)    if i >70:        dic[‘k1‘].append(i)    else:        dic[‘k2‘].append(i)print(dic)
二、尋找尋找列表中元素,移除每個元素的空格,並尋找以 a或A開頭 並且以 c 結尾的所有元素。    li = ["alec", " aric", "Alex", "Tony", "rain"]    tu = ("alec", " aric", "Alex", "Tony", "rain")     dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘,  "k3": "Alex", "k4": "Tony"}
li = ["alec", " aric", "Alex", "Tony", "rain"]tu = ("alec", " aric", "Alex", "Tony", "rain")dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘,  "k3": "Alex", "k4": "Tony"}ret = []for i in li:    i = i.strip()    if i.startswith(‘a‘or‘A‘) and i.endswith(‘c‘):        ret.append(i)for i in tu:    i = i.strip()    if i.startswith(‘a‘or‘A‘) and i.endswith(‘c‘):        ret.append(i)for i in dic:    i = i.strip()    if i.startswith(‘a‘or‘A‘) and i.endswith(‘c‘):        ret.append(i)print(ret)#結果:[‘alec‘, ‘aric‘, ‘alec‘, ‘aric‘]
三、 輸出商品列表,使用者輸入序號,顯示使用者選中的商品商品 li = ["手機", "電腦", ‘滑鼠墊‘, ‘遊艇‘]
li=["手機","電腦","滑鼠墊","遊艇"]print("0是手機,1是電腦,2是滑鼠墊,3是遊艇")num=input("請輸入數字:")if num==‘0‘:    print(li[0])elif num==‘1‘:    print(li[1])elif num==‘2‘:    print(li[2])elif num==‘3‘:    print(li[3])else:    print(‘‘)
四、購物車

功能要求:

  • 要求使用者輸入總資產,例如:2000
  • 顯示商品列表,讓使用者根據序號選擇商品,加入購物車
  • 購買,如果商品總額大於總資產,提示賬戶餘額不足,否則,購買成功。
  • 附加:可儲值、某商品移除購物車
goods = [    {"name": "電腦", "price": 1999},    {"name": "滑鼠", "price": 10},    {"name": "遊艇", "price": 20},    {"name": "美女", "price": 998},]print("輸入0購買電腦,輸入1購買滑鼠,輸入2購買遊艇,輸入3購買美女,輸入其他數值,結算購物車")li = []total = input("請輸入總金額:")while True:    num = input("輸入要購買的序號:")    if int(num) == 0:        li.append(goods[0][‘price‘])    elif int(num) == 1:        li.append(goods[1][‘price‘])    elif int(num) == 2:        li.append(goods[2][‘price‘])    elif int(num) == 3:        li.append(goods[3][‘price‘])    else:        breaksum = 0for i in li:    sum +=i    print(sum)if sum>int(total):    print("餘額不足")else:    print("購買成功")

 

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.