Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

While loop not working when you run the code

def chineseZodiac(year):    
    if (year - 2000) % 12 == 0:
       sign = 'Dragon'
    elif (year - 2000) % 12 == 1:
       sign = 'Snake'
    elif (year - 2000) % 12 == 2:
       sign = 'Horse'
    elif (year - 2000) % 12 == 3:
       sign = 'sheep'
    elif (year - 2000) % 12 == 4:
       sign = 'Monkey'
    elif (year - 2000) % 12 == 5:
       sign = 'Rooster'
    elif (year - 2000) % 12 == 6:
       sign = 'Dog'
    elif (year - 2000) % 12 == 7:
       sign = 'Pig'
    elif (year - 2000) % 12 == 8:
       sign = 'Rat'
    elif (year - 2000) % 12 == 9:
       sign = 'Ox'
    elif (year - 2000) % 12 == 10:
       sign = 'Tiger'
    else:
       sign = 'Hare'
    return sign
year = int(input("enter year:"))
while (year <= 1980 and year >= 2014):
    print("your chinese zodiac is ", chineseZodiac(year))

‘there is no error but the print under the while loop doesn’t show when you run the code…
please help me, im noob :-(‘

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Building on my comment. The immediate cause of your problem is there’s no year that can be less than 1980 and greater than 2014. Also because you have a while loop, so I think you want to continually ask for a year until a valid year is entered

def chineseZodiac(year):
        
    signs = {
        0: 'Dragon',
        1: 'Snake',
        2: 'Horse',
        3: 'sheep',
        4: 'Monkey',
        5: 'Rooster',
        6: 'Dog',
        7: 'Pig',
        8: 'Rat',
        9: 'Ox',
        10: 'Tiger',
        11: 'Hare'
    }
    
    return signs[( year - 2000 ) % 12]

year = 0

in_range = lambda year: (1980 <= year <= 2014)
while not in_range(year):
    year = int(input("enter year:"))
    
    if in_range(year):
        print("your chinese zodiac is", chineseZodiac(year))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading