Python學習的第二天

2021-09-25 07:43:37 字數 1905 閱讀 7919

with open(「passwd」, 「w」, encoding=(「utf-8」)) as f: //將賬號和密碼寫入檔案,永久儲存

json.dump(user, f)

i = 0

while i < 3:

username = input(「username:」)

password = input(「password:」)

if username == _username and password == _password:

print(「logining …」)

exit()

else: //輸入賬號和密碼超過3次後

if i == 2:

with open(「passwd」, 「a」) as f:

f.write("#")

print(「使用者賬號已鎖定…」)

else:

print(「username or password err…」)

print(「請在輸入一次…」)

i += 1

七,流程控制

1,break語句

break語句用來終止迴圈語句,即迴圈條件沒有false條件或者序列還沒被完全遞迴完,也會停止執行循

環語句。

2,continue語句

1)continue 語句跳出本次迴圈,而break跳出整個迴圈。

2)continue 語句用來告訴python跳過當前迴圈的剩餘語句,然後繼續進行下一輪迴圈。

3)continue語句用在while和for迴圈中。

3,pass語句

1)python pass 是空語句,是為了保持程式結構的完整性。

2)pass 不做任何事情,一般用做佔位語句。

八,購物車程式

product_list = [

(『robot』,200000),

(『macpro』,12000),

(『iphone8』,8888),

(『hello world』,1200),

]shopping_list =

user_salary=input(「請輸入你的工資:」)

if user_salary.isdigit():

user_salary = int(user_salary)

while true:

print(「商品如下:」)

for index,item in enumerate(product_list):

print (index,item)

user_choice = input("請輸入要購買的商品編號:")

if user_choice.isdigit():

user_choice = int(user_choice)

if user_choice < len(product_list) and user_choice > -1:

p_item = product_list[user_choice]

if user_salary>=p_item[1]:

user_salary-=p_item[1]

print("購買商品",p_item,"成功您的餘額為",user_salary,"元!" )

else:

print("您的餘額為",user_salary,"餘額不足以購買此商品,購買失敗!")

else:

print("並無此產品!")

elif user_choice == "q":

print("--------shopping list-------")

for i in shopping_list:

print(i)

exit()

else

print("請重新輸入正確數字")

Python學習第二天

1 python包含6種內建的序列,列表 元組 字串 unicode字串 buffer物件和xrange物件 2 列表和元組的主要區別在於,列表可以修改,元組則不能。在一般情況下,幾乎所有的情況下列表都可以替代元組 3 字串就是乙個由字元組成的序列,可以通過索引獲取序列中的元素,使用負數索引時,py...

python學習第二天

if 條件 滿足條件幹什麼1 滿足條件幹什麼2 滿足條件幹什麼3 else 不滿足條件幹什麼1 不滿足條件幹什麼2 或者是 if 條件1 x elif 條件2 x elif 條件3 x else x產生隨機數 import random random.randint 1,3 只在1,2,3中產生隨機...

學習Python 第二天

今天我們學習了迴圈和列表,在python中有兩種迴圈分別是for迴圈和while迴圈。for迴圈寫法 for 變數 in range 起點,終點,步長 迴圈體例題 列印實心菱形 for i in range 4,5 for j in range abs i print end for j in ra...