python日期判断星座_Python学习笔记 - 根据出生日期判断你的星座

知识点

strip()

语法:str.strip([chars])

参数chars:移除字符串头尾指定的字符序列

例如:

str.strip('qinjian'); # 去除首尾字符qinjian

split()

语法:str.split(str="", num=string.count(str))

参数str:分隔符,默认为所有的空字符,包括空格、换行(n)、制表符(t)等。

参数num:分割次数。默认为 -1, 即分隔所有。

例如:

tmp_str = '2020-12-01'

tmp_str.split('-') # 以“-”符号分割tmp_str字符串,得到如下列表

['2020', '12', '01']

实例

用户按照格式输入自己的生日,程序判断对应的星座以及星座符号# 星座判断列表

sdate = [20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22]

conts = ['魔羯座', '水瓶座', '双鱼座', '白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '魔羯座7']

sings = ['♑', '♒', '♓', '♈', '♉', '♊', '♋', '♌', '♍', '♎', '♏', '♐', '♑7']

# 输入生日

# strip(‘ ’)去除首尾空格

birth = input('请输入您的出生年月,格式为:2001-02-21').strip(' ')

# 分割年月日到列表

cbir = birth.split('-')

print(cbir)

# 提取月数据

cmonth = str(cbir[1])

# 提取日数据

cdate = str(cbir[2])

# 判断星座方法

def sign(month, date):

print(month, date)

# 所有列表是从0开始,所以有减1操作

if int(date) < sdate[int(month) - 1]:

print(conts[int(month)-1])

print(sings[int(month)-1])

else:

print(conts[int(month)])

print(sings[int(month)])

if __name__ == '__main__':

sign(cmonth, cdate)

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值