简体   繁体   中英

ValueError: invalid literal for int() with base 10: '' error

How do i resolve following error

ValueError: invalid literal for int() with base 10: '' error

this is example (int, int) -> str

Given two int values representing a month and a date, return a 3-character string that gives us what star sign a person born in that month and on that date belongs to. Use the SIGNS string (already defined for you at the top of this file) to figure this out.

    >>> find_astrological_sign(8, 24)
    'VIR'

    >>> find_astrological_sign(1, 15)
    'CAP'

SIGNS = '03,21-04,19=ARI;04,20-05,20=TAU;05,21-06,21=GEM;06,22-07,22=CAN;' + \
            '07,23-08,22=LEO;08,23-09,22=VIR;09,23-10,23=LIB;10,24-11,20=SCO;' + \
            '11,21-12,21=SAG;12,22-01,20=CAP;01,21-``02,21=AQU;02,22-03,20=PIS;'



def find_astrological_sign(month, date):



    x = SIGNS.split(";")
    for astro in x:
        if int(astro[0:2]) < month < int(astro[6:8]):
            if int(astro[3:5]) < date < 31 or 0 < date < int(astro[9:11]):
                return astro[12:15]

you need check if astro length is greater than 0 as split function will add empty string at the last position

also their is `` in your SIGNS which you dont need

Use below code

SIGNS = '03,21-04,19=ARI;04,20-05,20=TAU;05,21-06,21=GEM;06,22-07,22=CAN;' + \
            '07,23-08,22=LEO;08,23-09,22=VIR;09,23-10,23=LIB;10,24-11,20=SCO;' + \
            '11,21-12,21=SAG;12,22-01,20=CAP;01,21-02,21=AQU;02,22-03,20=PIS;'



def find_astrological_sign(month, date):

    x = SIGNS.split(";")
    for astro in x:
        if len(astro)>0:
            if int(astro[0:2]) < month < int(astro[6:8]):
                if int(astro[3:5]) < date < 31 or 0 < date < int(astro[9:11]):
                    return astro[12:15]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM