通过streamlit编写页面实现一个星座计算器。 即:输入出生年月日可以输出对应的星座生肖
以下是一个使用streamlit编写的星座计算器的示例代码:
import streamlit as st
# 星座名称和日期范围
constellations = {
"白羊座": ((3, 21), (4, 19)),
"金牛座": ((4, 20), (5, 20)),
"双子座": ((5, 21), (6, 21)),
"巨蟹座": ((6, 22), (7, 22)),
"狮子座": ((7, 23), (8, 22)),
"处女座": ((8, 23), (9, 22)),
"天秤座": ((9, 23), (10, 23)),
"天蝎座": ((10, 24), (11, 22)),
"射手座": ((11, 23), (12, 21)),
"摩羯座": ((12, 22), (1, 19)),
"水瓶座": ((1, 20), (2, 18)),
"双鱼座": ((2, 19), (3, 20))
}
# 生肖名称和起始年份
zodiacs = {
"鼠": 1900,
"牛": 1901,
"虎": 1902,
"兔": 1903,
"龙": 1904,
"蛇": 1905,
"马": 1906,
"羊": 1907,
"猴": 1908,
"鸡": 1909,
"狗": 1910,
"猪": 1911
}
def calculate_constellation(birth_date):
month, day = birth_date.month, birth_date.day
for constellation, date_range in constellations.items():
start_date, end_date = date_range
if (month == start_date[0] and day >= start_date[1]) or (month == end_date[0] and day <= end_date[1]):
return constellation
return None
def calculate_zodiac(birth_year):
for zodiac, start_year in zodiacs.items():
if birth_year >= start_year:
return zodiac
return None
# 页面布局
st.title("星座计算器")
birth_date = st.date_input("请选择您的出生日期")
if birth_date:
constellation = calculate_constellation(birth_date)
zodiac = calculate_zodiac(birth_date.year)
if constellation and zodiac:
st.write(f"您的星座是:{constellation}")
st.write(f"您的生肖是:{zodiac}")
else:
st.write("请输入有效的出生日期")
要运行该代码,你需要先安装streamlit库,可以使用以下命令进行安装:
pip install streamlit
然后在终端中运行以下命令来启动streamlit应用:
streamlit run app.py
该应用将在浏览器中打开,并且你可以选择你的出生日期,并查看对应的星座和生肖。
原文地址: http://www.cveoy.top/t/topic/jcU1 著作权归作者所有。请勿转载和采集!