python學習之day2學習筆記(2)

2022-09-12 13:00:24 字數 3874 閱讀 5358

#

字典格式:key-value

info=

print(info)#

無序輸出字典:

print(info.keys())#

輸出字典的key。輸出:dict_keys(['stu1', 'stu2', 'stu3'])

print(info.values())#

輸出字典的values。輸出:dict_values(['zhangsan', 'lisi', 'wangwu'])

print(info['

stu1

'])#

顯示"stu1"的資訊。輸出:zhangsan

print(info.get('

stu5

'))#

查詢,若無物件,輸出none。輸出:none

info['

stu1

']="張三"

#更改"stu1"的資訊。輸出:

print

(info)

#del

info.pop("

stu1")

print(info)#

輸出:b=

info.update(b)

#資料更新新增。輸出:

c=dict.fromkeys([6,7,8],"

test

")#通過乙個列表生成預設字典

print(c)#

輸出:

#

多級列表

info1=,

"stu2":

}print(info1)#

輸出(, 'stu2': },)

#info1['stu1']["name"][1]="耶耶耶耶張三"#更改資料,輸出:, 'stu2': }

#key在字典裡,則不會改變資料,否則就新增

info1.setdefault("

stu1

",)print(info1['

stu1

'])#

輸出:info1.setdefault("

stu3

",)print(info1['

stu3

'])#

輸出:

需求:啟動程式後,讓使用者輸入工資,然後列印商品列表

允許使用者根據商品編號購買商品

使用者選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒 

可隨時退出,退出時,列印已購買商品和餘額

shopping_list=

product_list = [["

iphone

",5800],["

mac pro

",12000],["

starbuck latte

",31],["

alex pythn

",81],["

bike

",800]]

salary = input("

輸入薪水:")

ifsalary.isdigit():

salary=int(salary)

while

true:

for index,item in

enumerate(product_list):

print

(index,item)

user_choice=input("

輸入選擇要買的商品的序號:")

ifuser_choice.isdigit():

user_choice=int(user_choice)

if user_choice>=0 and user_choice<=len(product_list):

p_item=product_list[user_choice]

if p_item[1]salary-=p_item[1]

print("

added %s into shopping cart,your current balance is \033[31;1m%s\033[0m

"%(p_item[0],salary))

else

:

print("

\033[41;1m你的餘額只剩%s啦,不夠啦\033[0m

"%salary)

else

:

print("

選擇的商品不存在")

else

:

print("結束"

)

#輸出資料

print("

------------shopping cart----------")

for i in

shopping_list:

print

(i)

print("

你的餘額:

",salary)

exit()

else

:

print("

結束")

要求: 

列印省、市、縣**選單

可返回上一級

可隨時退出程式

menu =,

'網易':{},

'google

':{}

},'中關村':,

'汽車之家

':{},

'youku

':{},

},'上地

':,},

},'昌平

':,'北航

':{},

},'天通苑

':{},

'回龍觀

':{},

},'朝陽

':{},

'東城':{},

},'上海':}

},'閘北':}

},'浦東

':{},

},'山東

':{},

}while

true:

for i in

menu:

print

(i) choice1=input("

你的選擇1>>:")

if choice1 in

menu:

while

true:

for i2 in

menu[choice1]:

print("\t"

,i2)

choice2=input("

你的選擇2>>:")

if choice2 in

menu[choice1]:

while

true:

for i3 in

menu[choice1][choice2]:

print("

\t\t

",i3)

choice3=input("

你的選擇3>>:")

if choice3 in

menu[choice1][choice2]:

for i4 in

menu[choice1][choice2][choice3]:

print("

\t\t

",i4)

choice4=input("

最後一層,按b返回")

if choice4=='b'

:

pass

if choice3=="b"

:

break

if choice2=="b"

:

break

python學習 day2 基礎學習

while 條件 塊執行流程 判斷條件是否為真,如果為真,執行 塊,執行完成 塊後,繼續判斷條件是否為真,如果為真,執行 塊.直到條件為假時,跳出迴圈體,結束迴圈。上圖的 執行流程為 step1 給count賦乙個初始值1,這個時候 count 1 step2 進入while迴圈,判斷迴圈條件 co...

Python學習之路Day2

message world print message 輸出結果為 world message hello world 輸出結果為 hello world print message 用引號 單 雙引號 括起來的都是字串 str1 this is a string str2 this is also...

Python基礎學習 Day2

python基礎學習 day2 pop 可以移除列表指定位置的物件並返回被刪除物件的值,注意該方法預設值為列表中的最後乙個元素且有返回值 del 移除列表中指定位置的多個物件,del 方式可以刪除整個列表,且列表刪除後無法訪問。拷貝分為copy 拷貝父物件,不會拷貝物件的內部的子物件和deepcop...