Exercise 49: Chinese Zodiac
(Solved, 40 Lines)
The Chinese zodiac assigns animals to years in a 12 year cycle. One 12 year cycle is
shown in the table below. The pattern repeats from there, with 2012 being another
year of the dragon, and 1999 being another year of the hare.
Write a program that reads a year from the user and displays the animal associated
with that year. Your program should work correctly for any year greater than or equal
to zero, not just the ones listed in the tabl
Exercise 49: Chinese Zodiac
## # Determine the animal associated with a year according to the Chinese zodiac. # # Read a year from the user year = int(input("Enter a year: ")) # Determine the animal associated with that year if year % 12 == 8: animal = "Dragon" elif year % 12 == 9: animal = "Snake" elif year % 12 == 10: animal = "Horse" elif year % 12 == 11: animal = "Sheep" elif year % 12 == 0: animal = "Monkey" elif year % 12 == 1: animal = "Rooster" elif year % 12 == 2: animal = "Dog" elif year % 12 == 3: animal = "Pig" elif year % 12 == 4: animal = "Rat" elif year % 12 == 5: animal = "Ox" elif year % 12 == 6: animal = "Tiger" elif year % 12 == 7: animal = "Hare" # Report the result print("%d is the year of the %s." % (year, animal))
The first part is the question and the second part is the solution
In the solution, l don’t quite understand it, if l take 2000/12, l get a remainder of 2 , not 8 and if l try executing this program, it shows
Traceback (most recent call last):
File “C:/Users/simmy/.PyCharmCE2018.2/config/scratches/test.py”, line 6, in
for years in cycle:
TypeError: ‘int’ object is not iterab
so l actually don’t know how to create a program for this, can l get a hint or something
Try starting with a pen and paper and simply laying out the groundwork for how you could solve this without coding. Like what are the actual steps you would take if someone gave you an integer year and asked you to determine what zodiac animal this was associated with. If you can do that then translating it into code becomes much easier.
so for every 12th year again l need to start counting from zero again, for the next cycle, now l don’t know how to put that into code
Is the printed solution correct?
It the output what the lesson wants you to output?
so this is the solution of the exercise and according to me l feel its wrong or l don’t understand it, and l have tried to create the code but not managed
Get some print statements in there and see for example what
year % 12 actually outputs. Relying too much on solutions that aren’t clear to you will keep causing the same problems again and again.
how did they know that year % 12 == 8, will always give us a dragon year or what l mean is how did they come up with this idea or formula
I guess it’s just a count. If you know the pattern (provided in your initial post) then you can simply index the names with a value between 0 and 11. So you can look up that first table and see that at index 5 (2005) we have the year of the Rooster. So any time your remainder results in 5 you can use the same index.
oh ok that helps