1. 程式人生 > >Python學習--day4

Python學習--day4

day4

一、常量

  Python並未提供如C/C++/Java一樣的const修飾符,換言之,python中沒有常量,python程式一般通過約定俗成的變數名全大寫的形式表示這是一個常量。

AGE = 78          #變數名全大寫
print(AGE)
View Code

 二、流程控制:

  1、流程控制之if判斷:
'''語法1:
if 條件:
    程式碼1
    程式碼2
    程式碼3
    ...
'''
# age_of_bk=30
# print('start.....')
#
#
inp_age=input('>>>: ') #inp_age='18' # inp_age=int(inp_age) # if inp_age == age_of_bk: # print('猜對了') # # print('end.....') '''語法2: if 條件: 程式碼1 程式碼2 程式碼3 ... else: 程式碼1 程式碼2 程式碼3 ... ''' # age=38 # gender='male' # is_beautiful=True # # if age >= 18 and age <= 25 and gender == 'female' and is_beautiful:
# print('開始表白。。。。') # # else: # print('阿姨好') # 語法3: if 條件1: 程式碼1 程式碼2 程式碼3 ... elif 條件4: 程式碼1 程式碼2 程式碼3 ... else: 程式碼1 程式碼2 程式碼3 ... ''' # score=input('your score>>: ') # score=int(score) # if score >=90: # print('優秀') # elif score >=80: # print('良好') # elif score >=70: # print('普通') # else: # print('很差')
''' 語法4: if 條件1: if 條件2: 程式碼1 程式碼2 程式碼3 ... 程式碼2 程式碼3 ''' # age=18 # gender='female' # is_beautiful=True # is_successful=True # # if age >= 18 and age <= 25 and gender == 'female' and is_beautiful: # print('開始表白。。。。') # if is_successful: # print('在一起') # else: # print('我逗你玩呢。。。') # else: # print('阿姨好')
View Code
  2、流程控制之while迴圈:
#1. while迴圈:條件迴圈
# I: 基本語法
# while 條件:
#     程式碼1
#     程式碼2
#     程式碼3
#     ...

# 示範
# name_of_bk='egon'
# pwd_of_bk='123'
#
# tag=True
# while tag:
#     inp_name=input('your name>>: ')
#     inp_pwd=input('your password>>: ')
#     if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
#         print('login successful')
#         tag=False
#     else:
#         print('username or password error')
#
#     print('other code...')

# II: while+break:break代表結束本層迴圈
# 示範:
# while True:
#     print(1)
#     break
#     print(2)
#     print(3)

# name_of_bk='egon'
# pwd_of_bk='123'
#
# while True:
#     inp_name=input('your name>>: ')
#     inp_pwd=input('your password>>: ')
#     if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
#         print('login successful')
#         break
#     else:
#         print('username or password error')
#
#     print('other code......')


# III: while + continue: continue代表結束本次迴圈,直接進入下一次
# 示範
# count=1
# while count < 6:
#     if count == 3:
#         count+=1
#         continue
#     print(count)
#     count+=1


# 輸錯三次退出
# name_of_bk='egon'
# pwd_of_bk='123'
#
# count=0
# while True:
#     if count == 3:
#         print('輸錯的次數過多。。。')
#         break
#     inp_name=input('your name>>: ')
#     inp_pwd=input('your password>>: ')
#     if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
#         print('login successful')
#         break
#     else:
#         print('username or password error')
#         count+=1 #count=3 輸錯3次


# IV:while + else
# count=0
# while True:
#     if count == 10:
#         break
#     print(count)
#     count+=1
#
# else:
#     print("else的子代塊只有在while迴圈沒有被break打斷的情況下才會執行")



# count=0
# while count <= 10:
#     print(count)
#     count+=1
#
# else:
#     print("else的子代塊只有在while迴圈沒有被break打斷的情況下才會執行")

# name_of_bk='egon'
# pwd_of_bk='123'
# 
# count=0
# tag=True
# while tag:
#     if count == 3:
#         print('輸錯的次數過多。。。')
#         break
#     inp_name=input('your name>>: ')
#     inp_pwd=input('your password>>: ')
#     if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
#         print('login successful')
#         while tag:
#             print("""
#             0 退出
#             1 購物
#             2 支付
#             3 檢視購物
#             """)
#             cmd=input('>>>: ')
#             if cmd == '0':
#                 tag=False
#                 continue
#             if cmd == '1':
#                 print('購物。。。。。。。')
#             elif cmd == '2':
#                 print('支付。。。。。')
#             elif cmd == '3':
#                 print('檢視購物車')
#             else:
#                 print('輸入錯誤的指令')
#     else:
#         print('username or password error')
#         count+=1 #count=3 輸錯3次
View Code