python if语句的应用

判断是否为奇数

e = int(input("请输入一个数"))
if(e%2==1):
    print(e,"是奇数")
else:
    print(e,"是偶数")

判断月份是哪个季度

yf=int(input("请输入月份"))
if(yf<=3and yf>0):
    print(yf,"月是第一季")
elif(yf<=6):
    print(yf,"月是第二季")
elif(yf<=9):
    print(yf,"月是第三季")
elif(yf<=12):
    print(yf,"月是第四季")
else:
    print("只有12个月")

求阶乘

求阶乘
p=1
jc=int(input("求阶乘"))
if jc!=0:
    for i in range(1, jc+1):
        p *= i
    print(jc, "的阶乘是", p)
else:print(jc,"的阶乘是",p)

判断当月有多少天

ln=int(input("请输入月份"))
if ln<=12 and ln>0:
    if ln < 8 and ln != 2:
        if ln % 2 == 1:
            c = 31;
        else:
            c = 30;
    else:
        if ln % 2 == 0:
            c = 31

        else:
            c = 30
    if ln == 2:
        c = 28
    print(ln, "月有", c, "天")
else:print("您输入的月份不规范!")

判断闰年

nf=int(input("请输入年份"))
if nf%4==0 or nf%100!=0 and nf%400==0:
    print(nf,"是闰年")
else:
    print(nf,"不是闰年")

判断闰年与输出月份结合

nf=int(input("请先输入年份:"))
if nf%4==0 and nf%100!=0 or nf%400==0:
    ln = int(input("请输入月份"))
    if ln <= 12 and ln > 0:
        if ln < 8 and ln != 2:
            if ln % 2 == 1:
                c = 31;
            else:
                c = 30;
        else:
            if ln % 2 == 0:
                c = 31

            else:
                c = 30
        if ln == 2:
            c = 29
        print(ln, "月有", c, "天")
    else:
        print("您输入的月份不规范!")
else:
    ln = int(input("请输入月份"))
    if ln <= 12 and ln > 0:
        if ln < 8 and ln != 2:
            if ln % 2 == 1:
                c = 31;
            else:
                c = 30;
        else:
            if ln % 2 == 0:
                c = 31

            else:
                c = 30
        if ln == 2:
            c = 28
        print(ln, "月有", c, "天")
    else:
        print("您输入的月份不规范!")

你可能感兴趣的:(python if语句的应用)