1. 程式人生 > >神奇語言 python while語句 邏輯運算 格式化

神奇語言 python while語句 邏輯運算 格式化

1while 迴圈

while 關鍵字 空格 條件 冒號

縮排體 迴圈體 

while 迴圈程式碼體現形式

例題:

設定一個理想數字比如:66,讓使用者輸入數字,如果比66大,則顯示猜測的結果大了
# ;如果比66小,則顯示猜測的結果小了;只有等於66,顯示猜測結果正確,然後退出迴圈。
shu = 66
while True:
    A=int(input("請輸入一個數字:"))
    if A > shu:
        print("輸入大")
    elif A< shu:
        print("輸入小了")
    else:
        
print("輸入正確") break

2邏輯運算

and :x and y ,當x = 0 時取值為x ,當x != 0時取值y(只要有一個假,取值取假的)

or : x or y . 當x = 0 時 取值為 y ,當x != 0 時取值x (or兩個都為假時取前一個,有一個為真時取真的為值)

not : 非假即真,非真即假

例題:

1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6  True
2)not 2 > 1 and 3 < 4 or 4 > 5 and
2 > 1 and 9 > 8 or 7 < 6 False

3 格式化:

%s : 適用於任何.

%d : 只適用於數字

f:字串 :{}

例題:

⽤戶登陸(三次輸錯機會)且每次輸錯誤時顯示剩餘錯誤次數(提示:使⽤字串格式化)
hu ="zxc"
ma = 123
count = 1
while count <= 3:
    yong = input("請輸入使用者:")
    mi = int(input("請輸入密碼:"))
    if yong == hu and mi == ma:
        print("輸入正確")
    
else: print("輸入錯誤") print(f"你還剩%s次機會",(3-count)) count += 1

4.初識編碼

ascii 美國 256 沒有中文
一個位元組 8位
gbk 中國
中文 2位元組 16位
英文 1位元組 8位
unicode 萬國碼
2個位元組 16位
4個位元組 32位
utf-8 可變編碼
英文 1位元組 8位
歐洲 2位元組 16位
亞洲 3位元組 24位