開始跟着視頻學python,第五天mark【與或非、大於小於等於】

今天視頻開始位置66:40
邏輯運算符。#就是與、或、非什麼的。

has_high_income = True
has_good_credit = False

if has_good_credit and has_high_income:   #AND  與
    print('eligible for loan')
else:
    print('no')

輸出爲:

no

not 非

has_high_income = True
has_bad_credit = False

if has_high_income and not has_bad_credit:
    print('eligible for loan')
else:
    print('no')

小知識:

#優先級not>and>or
#and中含0,返回0; 均爲非0時,返回後一個值
如3 and 4,返回4
#or中, 至少有一個非0時,返回第一個非0

大於 >
小於 <
等於 ==
大於等於 >=
不等於 !=

小練習:
如果名字小於3個字符,輸出 太短
如果名字 大於20字符,輸出太長
其他,輸出 好名字

name = input('please input your name: ')    # input() 函數接受一個標準輸入數據,返回爲 string 類型
a = len(name)
if a > 20:      #也可直接  if len(name) > 20
    print("too long")
elif a <3:
    print("too short")
else:
    print("good name")

溫馨提示:input輸入返回的是字符串,如果是想輸入數字進行計算的話需要用int()將其轉換,而且int()不能轉換字母。

練習:
輸入體重,再輸入單位,輸出另一單位體重
如輸入 70 ,再輸入kg ,輸出 140jin
實現如下:

weight = int(input("may i konw your weight number?  "))   #此處要求輸入數字,其實應該加上防錯輸程序
danwei = input("kg or jin ?  ")
if danwei == 'kg':
    print(f"your weight is {weight * 2} jin.")
else:
    print(f"your weight is {weight / 2} kg.")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章