简体   繁体   中英

Why am I getting ValueError: invalid literal for int() with base 10: ''

When I run this program, i get the error, ValueError: invalid literal for int() with base 10: '', I feel like it's to do with the int and str conversions but I'm really not too sure, any help appreciated :)

CalendarDict = {1:"January", 2:"February", 3:"March", 4:"April", 5:"May", 
6:"June", 7:"July", 8:"August", 9:"September", 10:"October", 11:"Novemeber", 
12:"December"}

InputError = True
while InputError:
    try:
        BirthDate = str(input("Enter Birth Date in format DDMMYY - "))
    except ValueError:
        print("Error - Numbers in format DDMMYY only")
        InputError = False

DD = BirthDate[0:2] 
MM = BirthDate[3:4]
YY = BirthDate[4:6]

if MM == BirthDate[3:4]:
   print("Your Birth Month is - ", (CalendarDict[int(MM)]))

I would rather put this in a comment but don't have enough reps, so here goes.

Firstly, array slicing in Python requires you to give the numbers in a format [a:b] , where a is the index of the first character you want to get and b is the index of the character upto but including which you want to get your characters, so variable MM should be BirthDate[2:4] . 包括您要获取的字符得到你的角色,因此变量MM应该是BirthDate[2:4]

Next, to check whether something qualifies your "DDMMYY" requirement, you should probably be using int(input("Enter your DOB )) because anyone can enter random text and get away with it if you use the str() function to convert it into a string (because I believe you are looking for integral input)

Also, as mentioned in one of the comments, try putting InputError=False in the try part instead of the except part.

So code will look like this:

CalendarDict = {1:"January", 2:"February", 3:"March", 4:"April", 5:"May", 
6:"June", 7:"July", 8:"August", 9:"September", 10:"October", 11:"Novemeber", 
12:"December"}

InputError = True
while InputError:
    try:
        BirthDate = int(input("Enter Birth Date in format DDMMYY - ")) # change to int() from str()
        InputError = False # set error to false since the above line got evaluated
    except ValueError:
        print("Error - Numbers in format DDMMYY only")

DD = BirthDate[0:2] 
MM = BirthDate[2:4]
YY = BirthDate[4:6]

print("Your Birth Month is - ", (CalendarDict[MM])) # converting into integer is not required since it already is one!  

You can use the datetime module to do what you want quite efficiently.

import datetime

while True:
    try:
        birthDate = datetime.datetime.strptime(input("Enter Birth Date in format DD/MM/YYYY - "), "%d/%m/%Y")
        break
    except ValueError as ve:
        print(ve)
        continue

print("Your Birth Month is - {}".format(birthDate.strftime("%B")))

This results in the usage of:

Enter Birth Date in format DD/MM/YYYY - 31/10/2000
Your Birth Month is - October

datetime is quite powerful, especially the provided .strptime , for parsing dates, and .strftime for providing various outputs. I'd advise you to read the documentation if you plan on working with input, output and dates. datetime is easily extensible to more complex tasks with dates.

If you're using Python2, change input to raw_input .

I've also removed your if statement - it seemed to be checking MM against the definition of MM. Note that CalendarDict is unecessary as you can use the power of datetime . I've changed your while loop to just use control flow statement, rather than a variable.

Also a general tip: use camelCasing or underscore_casing for variables, as CapitalCasing is generally reserved for classes.

The bit that was tripping you up was the slice notation , as noted by others. Here's a version that seems to do what you want:

CalendarDict = {1:"January", 2:"February", 3:"March", 4:"April", 5:"May", 
6:"June", 7:"July", 8:"August", 9:"September", 10:"October", 11:"Novemeber", 
12:"December"}

while True:
    try:
        BirthDate = str(input("Enter Birth Date in format DDMMYY - "))
        break
    except ValueError:
        print("Error - Numbers in format DDMMYY only")

DD = BirthDate[0:2] 
MM = BirthDate[2:4]
YY = BirthDate[4:]

print("Your Birth Month is - ", (CalendarDict[int(MM)]))

Note how the start and end positions match up.

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