我需要一个代表黄道十二宫日历的清单。它是接受用户输入月和日的小程序的一部分。有没有办法做到这一点?我仍然处于逻辑尿布中,当谈到python时,它可能还在子宫里。我也已经被告知我不是非常pythonic ...我知道。
month = ["January","February","March","April","May","June",
"July","August","September","October","November","December"]
#The following is the list I am attempting to create
#I need it to represent 365 days but I don't want to type
#each zodiac sign thirty times. It would look ridiculous
#and would probably be considered illogical.
zodiac = [['Capricorn']*19,['Aquarius']*30,['Pisces']*30]
#You probably know with out running the above list that it only
#has three elements zodiac[0],[1],[2]... I need it to have 79.
#Is this possible?
dayOfYear= [0,31,59,90,120,151,181,212,243,273,304,334]
zodiacDay = int
birthmonth = int(raw_input('Enter your birth month(1-12): ')) - 1
while birthmonth <= 12:
birthday = int(raw_input('Enter your birth day(1-31): '))
while birthday < 32:
zodiacDay = dayOfYear[birthmonth] + birthday
print"""
You were born on the %r day of the year,
which is the %r of %s. Your Zodiac sign is %s.
""" % (dayOfYear[birthmonth] + birthday,birthday,month[birthmonth],
zodiac[zodiacDay])
birthmonth = int(raw_input('Enter your birth month(1-12): ')) - 1
答案 0 :(得分:0)
关于如何组合包含固定值的多个列表的主要问题:
zodiac = ['Capricorn']*19 + ['Aquarius']*30 + ['Pisces']*30
等。这将连接字符串列表,而不是创建字符串列表列表。还有其他方法,但在我看来这是最简单的,非常适合这种情况。
对于其他建议 - 如果您希望使用一年中的某一天作为预先计算的值列表的索引,您可能会发现内置datetime.date
类的toordinal
方法很有用。< / p>
正如Marcin建议的那样,使用范围通常是我如何解决这类问题 - 按最早的数字存储符号值的索引,并遍历该列表,直到找到小于或等于当天的值给出的数字。但使用365元素列表来存储值并不可怕。
如果你这样做不仅仅是一个学习练习,那么一定要考虑闰年。我对占星术的了解不足以确切知道在那里推荐什么。