邁向大神之路day006
阿新 • • 發佈:2018-11-09
day006
python 2的區別
- print ‘hllo’ #
- xrange 生成器
- raw_input()
兩個地址命令
is 比較的是記憶體地址 id 是記憶體地址
地址相關知識
小資料池 節省空間 適用於: 數字和字串 -5 --256 公共適應地址是一樣的
str :如果想同一個同一個地址條件
1.字串不能有特殊字元 2.s*20 一個字元 還是同一個地址
python3的一些編碼知識
str 在記憶體中用的是unicode 編碼
bytes 型別 和str區別,編碼方式不同
轉化形式 str ->bytes
對於英文
str s=‘alex’
bytes s=b‘alex’
對於中文
str s=‘中文’
bytes s=b‘x\e91\e91’–utf-8 3個位元組表示一箇中文 gbk是兩個位元組
str 轉化為 bytes
sl=‘alex’
sl1=s1.encode(‘utf-8’)
各個編碼之間的二進位制 不能相互識別 會產生亂碼
檔案儲存和傳輸 不是unicode 只是utf-8和gbk asii
案列購物車:
li=[{'name':'apple','price':10},
{'name' :'banana','price':20} ] #生成一個商品列表
shopping_car={ }
print('歡迎光臨本商店:')
money=input("讓我看看你的貝殼:")
if money.isdigit() and int(money)>0:
for i,k in enumerate(li):
print('序號{},商品{} ,價格'.format(i,k['name'],k['price']))
choose=input("請輸入你要購買的商品")
if choose.isdigit() and int(choose) < len(li):
num=input("你要購買商品數量")
if num.isdigit():
if int(money)>li[int(choose)]['price']*int(num) :
money=int(money)-li[int(choose)]['price']*int(num)
if li[int(choose) ]['name'] in shopping_car:
shopping_car[li[int(choose)]['name']]=int(num)+ shopping_car[li[int(choose)]['name']]
else:
shopping_car[li[int(choose)]['name']]=int(num)
print("購物車的商品有{}:你的餘額為:{}".format(shopping_car,money))
else:
print("沒有錢你來幹什麼:")
else:
print("請重新輸入")