風無定,人無常,人生如浮萍,聚散兩茫茫——元組類型、字典類型的內置方法,第九天

想想自己混的也是挺糟心的,從一個毫無疑問一定可以畢業的學生到了一個也許只能我一個不能畢業的學生;從一個不需要誰太擔心的人,變成一個老頭或者小孩?總而言之,如人生如浮萍,聚散兩茫茫所說,一切都存在着未知,一件事沒確定之前都是很難判斷的。風無定,人無常,人生如浮萍,聚散兩茫茫。加油吧!希望藉此機會能夠跳出chong圍攀上龍鳳枝,真正拼一把,希望吧!

goods=[
{'商品編號':'1','商品名稱':'蘋果頂配電腦','商品價格':100001,},
{'商品編號':'2','商品名稱':'聯想低配電腦','商品價格':10000},
{'商品編號':'3','商品名稱':'蘋果手機','商品價格':10},
{'商品編號':'4','商品名稱':'邁凱倫 720s','商品價格':10000000},
]
name = 'cyd'
psd = '123'
salary = 0
count1 = 0
count2 = 0
count3 = 0
count4 = 0
shopping_lists = []
shopping_dicts = {}
print("您隨時可以按’q'退出該程序。")

while count1 < 3:
	count1  += 1
	inp_name = input('請輸入您的名字: ').strip()
	if inp_name == 'q':
		count1 = 3
		break
	inp_psd = input('請輸入您的密碼: ').strip()
	if inp_psd == 'q':
		count1 = 3
		break
	if name == inp_name and psd == inp_psd:
		print('歡迎VIP %s!!!\n' %inp_name)

		while count2 < 3:
			salary = input('請輸入您想要貸款的金額(金額 < 100000): ').strip()
			if str(salary) == 'q':
				count1 = 3
				count2 = 3
				break
			if salary.isdigit():
				pass
			else:
				print("貸款金額輸入請使用數字輸入: ")
				continue
			if int(salary) < 100000:
				pass
			else:
				print("抱歉!你沒有這麼高的額度。")
				continue

			while count3 < 3:
				salary = int(salary)
				print('\n' + ' '*25 + '商品清單' + '\n')
				for good in goods:
					print('-'*10 + str(good) + '-'*10)
				print("您目前剩餘金額: " + str(salary))
				label = input("請輸入您想購買的商品編號: ").strip()
				if label == 'q':
					count1 = 3
					count2 = 3
					count3 = 3
					count4 = 3
					break
				if label.isdigit():
					pass
				if int(label) < len(goods) + 1  and int(label) > 0:
					pass
				else:
					print("您輸入的商品不存在,商品編號請使用現有商品編號數字輸入,請重新輸入你想要購買的商品: ")
					count3 = 0
					continue
				good_count = input("請輸入您想購買的商品數量: ").strip()
				if good_count == 'q':
					count1 = 3
					count2 = 3
					count3 = 3
					count4 = 3
					break
				if good_count.isdigit():
					pass
				else:
					print("商品數量請使用整數,請重新輸入您想購買的商品數量: ")
					count2 = 0
					continue
				print("您購買的是: %s * %s" %(goods[int(label) - 1]['商品名稱'], good_count))
				for good in goods:
					if label in good['商品編號']:
						if salary - good['商品價格']*int(good_count) < 0:
							print("您的餘額不足:")
							recharge = input('''
							1.輸入'有德無敵帥'進行充值,充值金額爲:10000000(一輛邁凱倫)\n
							2.輸入'我已向支付寶15306815606轉賬'並向支付寶賬戶15306815606充值¥10,充值金額:1000000\n
							3.輸入'3',充值金額:1000000\n
							4.輸入'q'退出購物車\n
							請輸入您想要充值的方法: ''').strip()
							if recharge == 'q':
								count1 = 3
								count2 = 3
								count3 = 3
								count4 = 3
								break
							if recharge == '有德無敵帥':
								print("大佬我向你致敬!!!"* 3)
								salary = 10000000 + salary
								continue
							if recharge == '我已向15306815606支付寶轉賬':
								print("謝謝您的支持!!!"* 2)
								salary = 1000000 + salary
								continue
							if recharge == '3':
								print('''\t\t\t\t\t小氣鬼喝涼水,老師打你你歪嘴,\n
								小氣鬼喝涼水,喝了涼水變魔鬼,\n
								小氣鬼喝涼水,娶個老婆四條腿,\n
								小氣鬼喝涼水,生個兒子沒大腿,\n
								小氣鬼喝涼水,砸破罐子,割破嘴,\n
								小氣鬼,喝涼水,回家抱着老婆親不了嘴。\n''')
								salary = 100000 + salary
								continue
							else:
								print("輸出有誤,請重新輸入,需用數字輸入序列號!")
								continue
						else:
							salary = salary - good['商品價格']*int(good_count)
							print("您的餘額還有: %s" %salary)
							for shopping_list in shopping_lists:
								if goods[int(label) - 1]['商品名稱'] == shopping_dicts['商品名稱']:
									shopping_dicts['商品數量'] += int(good_count)
									break
							else:
								shopping_dicts = {'商品名稱': good['商品名稱'], '商品數量': int(good_count)}
								shopping_lists.append(shopping_dicts)
							print("你當前購物車現有物品: %s" %shopping_lists[:])

							while count4 < 1:
								quit2 = input("如果還想購買請按‘c’,否按'q'退出程序").strip()
								if quit2 == 'c':
									count3 = 0
									count4 = 0
									break
								elif quit2 == 'q':
									count1 = 3
									count2 = 3
									count3 = 3
									count4 = 3
									break
								else:
									print("您輸入有誤,請重新輸入。")
									count4 = 0
	else:
		print("請重新輸入您的賬號密碼。")
		if count1 == 3:
			break

	print("\n您已經成功購買: " + str(shopping_lists[:]))
	print("您還有餘額: %s" % salary)

附代碼:終極購物車代碼。

第八天

元組類型

元組:元組是不可變的列表,能存入多個值,但多個值只有取的需求,而沒有改的需求】

定義方式:在()內用逗號隔開任意類型的值

強調:當元組只有一個值的時候,務必加上一個逗號

x=('egon',)
print(type(x))

常用操作和內置方法:

1.按照索引取值(可以正向取,也可以反向取),只能取

2.切片(顧頭不顧尾, 步長)

names=('alex','egon','wxx','lxx','cxxx')
print(names[1:3])

3.長度len()

names=('alex','egon','wxx','lxx','cxxx')
print(len(names))

4.成員運算in 和 not in

names=('alex','egon','wxx','lxx','cxxx')
print('alex' in names)

5.循環

names=('alex','egon','wxx','lxx','cxxx')
for item in names:
    print(item)

元組需要總結的方法:

1,del刪除

names=('alex','egon','wxx','lxx','cxxx','lxx')
del names[0]

2.數出元組內值的個數

print(names.count('lxx'))

3.檢索元組內值的位置

print(names.index('wxx',0,3))

元組總結:

1.存一個值或多個值,存的值可以是任意類型的

2.有序

3.不可變

注意:列表可變的底層原理:

指的是索引所對應的內存地址是可以改變的

元組不可變的底層原理:

指的是索引所對應的值內存地址是不可以改變的,或者反過來說,只要索引對應值的內存地址沒有改變,那麼元組始終是沒有改變的


字典類型:

value通常可以是任意類型,key 通常是不可變類型,但大多數是字符串類型

優先掌握的操作和內置方法:

1.按key取值,可存可取

d={'x':1,'y':2}
d['x']=100
print(d)
d['z']=3
print(d)

2.長度len,

d={'x':1,'y':2}
print(len(d))

3.成員運算in 和 not in

d={'x':1,'y':2}
print('x' in d)

4.刪除。pop和del類似於列表

d={'x':1,'y':2}
del d['x']
print(d)
res=d.pop('y')
print(d)
print(res)

5.popitem()隨機刪除字典中的一個鍵值對

res=d.popitem()
print(d)
print(res)

6.鍵keys()值values()和鍵值對items()

msg_dic={
'apple':10,
'tesla':100000,
'mac':3000,
'lenovo':30000,
'chicken':10,
}
names=[]
for k in msg_dic:
    names.append(k)
print(names)
values=[]
for k in msg_dic:
    values.append(msg_dic[k])
print(values)

keys=msg_dic.keys()
print(keys)
for k in keys:
    print(k)=
l=list(keys)
print(l)

vals=msg_dic.values()
print(vals)
print(list(vals))

print(msg_dic.items())
print(list(msg_dic.items()))

7.循環

只取key
for k in msg_dic:
    print(k,msg_dic[k])

for k in msg_dic.keys():  #keys可加可不加
    print(k,msg_dic[k])

只取value
for v in msg_dic.values():
    print(v)

keyvalue
for k,v in msg_dic.items(): #k,v=('apple', 10)
    print(k,v)

需要掌握的內置方法:

1.get()獲取某個不存在的值的時候返回None,不會報錯

d={'x':1,'y':2,'z':3}
v=d.get('xxxx')
print(v)
print(d['xxxxxxx'])

2.update(),把一個字典加入另一個字典中

d={'x':1,'y':2,'z':3}
d1={'a':1,'x':1111111}
d.update(d1)
print(d)

3.fromkeys,可以把一個列表初始化一個初始字典

l=['name','age','sex']
d={}
for k in l:
    d[k]=None
d=dict.fromkeys(l,None)
print(d)

4.setdefault

info.setdefault
info={'name':'egon','age':18,'sex':'male'}

如果字典中有setdefault指定的key,那麼不改該key對應的值,返回原的value
res=info.setdefault('name','EGON_NB')
print(info)
print(res)

如果字典中沒有setdefault指定的key,那麼增加一個key:value,返回新的value
info.setdefault('height',1.80)
print(info)

info={'age':18,'sex':'male'}
v=info.setdefault('name','浩哥')
print(v)

setdefault控制循環避免循環重複的值

s='hello alex alex say hello sb sb'
l=s.split()
print(l)
d={}
for word in l: #word=  'hello'
    if word not in d:
        d[word]=1 #{'hello':2, 'alex':2,'say':1}
    else:
        d[word]+=1
print(d)

s='hello alex alex say hello sb sb'
l=s.split()
print(l)
d={}

d={'hello':2,'alex':2}
for word in l: #word='alex'
    d[word]=l.count(word) #d['alex']=2
    d.setdefault(word,l.count(word))




發佈了43 篇原創文章 · 獲贊 8 · 訪問量 8789
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章