Python星座判斷

在Python中,我們可以通過編寫一個簡單的程式來判斷一個人的星座。星座是根據出生日期來確定的,以下是實現這一功能的代碼示例:

def zodiac_sign(month, day):
    if (month == 1 and day >= 20) or (month == 2 and day <= 18):
        return "水瓶座"
    elif (month == 2 and day >= 19) or (month == 3 and day <= 20):
        return "雙魚座"
    elif (month == 3 and day >= 21) or (month == 4 and day <= 19):
        return "牡羊座"
    elif (month == 4 and day >= 20) or (month == 5 and day <= 20):
        return "金牛座"
    elif (month == 5 and day >= 21) or (month == 6 and day <= 21):
        return "雙子座"
    elif (month == 6 and day >= 22) or (month == 7 and day <= 22):
        return "巨蟹座"
    elif (month == 7 and day >= 23) or (month == 8 and day <= 22):
        return "獅子座"
    elif (month == 8 and day >= 23) or (month == 9 and day <= 22):
        return "處女座"
    elif (month == 9 and day >= 23) or (month == 10 and day <= 23):
        return "天秤座"
    elif (month == 10 and day >= 24) or (month == 11 and day <= 22):
        return "天蠍座"
    elif (month == 11 and day >= 23) or (month == 12 and day <= 21):
        return "射手座"
    elif (month == 12 and day >= 22) or (month == 1 and day <= 19):
        return "摩羯座"
    else:
        return "日期無效"

# 示例使用
month = int(input("請輸入月份(1-12):"))
day = int(input("請輸入日期:"))
sign = zodiac_sign(month, day)
print(f"你的星座是:{sign}")

這個程式首先定義了一個函式 zodiac_sign,它接受月份和日期作為參數,並根據這些參數返回對應的星座。然後,程式通過用戶輸入獲取月份和日期,並調用 zodiac_sign 函式來確定星座,最後輸出結果。

需要注意的是,星座的日期範圍是基於西方占星學的標準,不同文化和地區可能有不同的星座劃分方式。

周公解夢大全查詢

相關解夢: