🔒 Closed Zodiac Sign Generator by Year[HELP]

Status
Not open for further replies.

cr4zyfr0g

Eternal Poster
Hello PHC Fam, Ano po kaya idadagdag ko dito na code? Ito po yung isa sa pinapagawa samin.
1.Year must be from included 1980-2014. Should the user inputs outside the range, the program will throw a ValueError.
Python:
Year = int(input("Year: "))
if (Year - 2000) % 12 == 0:
   chinesezodiac_sign = 'Dragon'
elif (Year - 2000) % 12 == 1:
    chinesezodiac_sign = 'Snake'
elif (Year - 2000) % 12 == 2:
    chinesezodiac_sign = 'Horse'
elif (Year - 2000) % 12 == 3:
    chinesezodiac_sign = 'sheep'
elif (Year - 2000) % 12 == 4:
    chinesezodiac_sign = 'Monkey'
elif (Year - 2000) % 12 == 5:
    chinesezodiac_sign = 'Rooster'
elif (Year - 2000) % 12 == 6:
    chinesezodiac_sign = 'Dog'
elif (Year - 2000) % 12 == 7:
    chinesezodiac_sign = 'Pig'
elif (Year - 2000) % 12 == 8:
    chinesezodiac_sign = 'Rat'
elif (Year - 2000) % 12 == 9:
    chinesezodiac_sign = 'Ox'
elif (Year - 2000) % 12 == 10:
    chinesezodiac_sign = 'Tiger'
else:
    chinesezodiac_sign = 'Hare'
print("Your Chinese Zodiac is", chinesezodiac_sign)
Screenshot_2022_0403_134936.webp
 
Ahm pwede mo siguro iadd yung pag check kung valid ba yung year
and pwede mo rin adopt yung ginawa ko instead of if else statement, dictionary nalang :)

Python:
while True:
    year = int(input('Input Year: '))
    if year in range(1980, 2014):
        break
    print('Year must be 1980-2014 only')

zodiacs = {
    0: 'Dragon',
    1: 'Snake',
    2: 'Horse',
    3: 'Sheep',
    4: 'Monkey',
    5: 'Rooster',
    6: 'Dog',
    7: 'Pig',
    8: 'Rat',
    9: 'Ox',
    10: 'Tiger'
}
zodiac_sign = (year - 2000) % 12
default_zodiac_sign = 'Hare'
chinese_zodiac_sign = zodiacs.get(zodiac_sign, default_zodiac_sign)

print('Zodiac Sign:', zodiac_sign)
print('Your Chinese Zodiac is:', chinese_zodiac_sign)

Output:

Code:
Input Year: 123
Year must be 1980-2014 only
Input Year: 1970
Year must be 1980-2014 only
Input Year: 1980
Zodiac Sign: 4
Your Chinese Zodiac is: Monkey

But since you want to throw an error, ganito gawin mo instead of While True


Python:
year = int(input('Input Year: '))
if year not in range(1980, 2014):
    raise ValueError(f'Invalid year: {year}. Year must be 1980-2014 only')

Code:
Input Year: 123
Traceback (most recent call last):
  File "C:\Users\ADMIN\Desktop\python\zodiac.py", line 4, in <module>
    raise ValueError(f'Invalid year: {year}. Year must be 1980-2014 only')
ValueError: Invalid year: 123. Year must be 1980-2014 only
 
Ahm pwede mo siguro iadd yung pag check kung valid ba yung year
and pwede mo rin adopt yung ginawa ko instead of if else statement, dictionary nalang :)

Python:
while True:
    year = int(input('Input Year: '))
    if year in range(1980, 2014):
        break
    print('Year must be 1980-2014 only')

zodiacs = {
    0: 'Dragon',
    1: 'Snake',
    2: 'Horse',
    3: 'Sheep',
    4: 'Monkey',
    5: 'Rooster',
    6: 'Dog',
    7: 'Pig',
    8: 'Rat',
    9: 'Ox',
    10: 'Tiger'
}
zodiac_sign = (year - 2000) % 12
default_zodiac_sign = 'Hare'
chinese_zodiac_sign = zodiacs.get(zodiac_sign, default_zodiac_sign)

print('Zodiac Sign:', zodiac_sign)
print('Your Chinese Zodiac is:', chinese_zodiac_sign)

Output:

Code:
Input Year: 123
Year must be 1980-2014 only
Input Year: 1970
Year must be 1980-2014 only
Input Year: 1980
Zodiac Sign: 4
Your Chinese Zodiac is: Monkey

But since you want to throw an error, ganito gawin mo instead of While True


Python:
year = int(input('Input Year: '))
if year not in range(1980, 2014):
    raise ValueError(f'Invalid year: {year}. Year must be 1980-2014 only')

Code:
Input Year: 123
Traceback (most recent call last):
  File "C:\Users\ADMIN\Desktop\python\zodiac.py", line 4, in <module>
    raise ValueError(f'Invalid year: {year}. Year must be 1980-2014 only')
ValueError: Invalid year: 123. Year must be 1980-2014 only
Maraming salamat lods
 
Status
Not open for further replies.

About this Thread

  • 3
    Replies
  • 669
    Views
  • 2
    Participants
Last reply from:
zackmark29

Trending Topics

Online now

Members online
799
Guests online
1,200
Total visitors
1,999

Forum statistics

Threads
2,286,904
Posts
29,041,069
Members
1,217,234
Latest member
jordy0943
Back
Top