代码:
def shengxiao(year): zodiac=["鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"] i= (year-1900)%12 j=zodiac[i] return j myyear=int(input("Please enter your year until enter 0:")) while myyear!=0: print("Your zodiac is:",shengxiao(myyear) ) myyear=int(input("Please enter your year until enter 0:"))
效果:
Please enter your year until enter 0:1984 Your zodiac is: 鼠 Please enter your year until enter 0:1992 Your zodiac is: 猴 Please enter your year until enter 0:1983 Your zodiac is: 猪 Please enter your year until enter 0:1981 Your zodiac is: 鸡 Please enter your year until enter 0:1999 Your zodiac is: 兔 Please enter your year until enter 0:1996 Your zodiac is: 鼠 Please enter your year until enter 0:2008 Your zodiac is: 鼠 Please enter your year until enter 0:0
总结:
- 列表有X元素,就取X的模,这样X[0],对应第一个元素,X[X-1]对应第X个元素;
- 这里的循环条件,myyear!=0,一直调用函数,一直输出提示语,直到输入0结束,可以改编成随机点名。