1. 程式人生 > 實用技巧 >Python_note_004(Python中的選擇結構,if語句)

Python_note_004(Python中的選擇結構,if語句)

選擇結構

  • 選擇結構

    #選擇結構 給定一個金額,輸入要取出的金額
    money=500
    newMoney=int(input('請輸入取款金額'))
    if money>=newMoney:
        money=money-newMoney
        print('取款金額為:'+str(newMoney)+',剩餘金額為:'+str(money))
    else:
        print('使用者餘額不足!!!')
    # 輸入一個數 判斷是奇數還是偶數 雙分支結構
    n=int(input('請輸入一個整數:'))
    if n%2==0:
        print(str(n)+"是偶數")
    else:
        print(str(n)+"是奇數")
        
    # 多分支結構********************
    i=int(input('請輸入學生的分數'))
    if  60<=i<70:
        print('c')
    elif i<60 and i>=0:
        print('D')
    elif i>=70 and i<80:
        print('B')
    elif i>=80 and i<=100:
        print('A')
    else :
        print('成績有誤')
        
    #巢狀if++++++++++++++++++++ 是會員打八折
    status=int(input('是否是會員?1:是會員,2:不是會員')) # 1:是會員,,2:不是會員
    money=float(input('購物金額是多少?'))
    if status==1:
        if money>=200:
            print('打八折',money*0.8)
        elif 200>money>=100:
            print('打9折',money*0.8)
        else:
            print('不打折')
    else:
        print('不打折')
        
    # 條件表示式+++++++++++
    num_a=int(input('輸入一個數'))
    num_b=int(input('輸入一個數'))
    print(str(num_a)+'大於等於'+str(num_b) if num_a>=num_b else str(num_a)+'小於'+str(num_b))