0

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)]))
Emily
  • 7
  • 1
  • 3
  • 1
    I have no even idea what language it is, but it is obvious to me that `MM` is containing some string which cannot represent an integer in base 10. Print it to see. – Eugene Sh. Aug 23 '17 at 15:34
  • Oh no sorry i forgot to put that, its python! – Emily Aug 23 '17 at 15:35
  • 1
    @Emily You're trying to convert an empty string to a number using `int`. That obviously won't work. You need to debug to find out where you're trying to do that. Since you only use `int` once, it should be easy to track down. – Carcigenicate Aug 23 '17 at 15:35
  • 2
    It may be useful to know that slicing outside of the range of a string or list in Python fails silently: `"a"[3:4] == ""`. – Izaak van Dongen Aug 23 '17 at 15:40
  • Okay thanks, what is base 10? – Emily Aug 23 '17 at 15:41
  • @Emily The base of the numbers. People count in base 10 in everyday life, meaning using 10 numbers (0 to 9). Same as the base 10 you would have learned in school. Base 2 is binary, since there's only 2 numbers (0 and 1), and base 16 is hexadecimal, since there are 16 different numbers (0 to F). – Carcigenicate Aug 23 '17 at 15:42
  • Your code is an infinite loop until you enter "bad data", among other things, try putting `InputErrors = True` after the `BirthDate` assignment. – MooingRawr Aug 23 '17 at 15:43
  • Base 10 is the numbering system based on the digits 0..9 -- it's also called "decimal". There are other bases like base 2 with only the digits 0 and 1, which is often called "binary". – martineau Aug 23 '17 at 15:44
  • 2
    Is this Python 2 or 3? I can't quite figure it out, and this will change the behaviour of `input` quite drastically. – Izaak van Dongen Aug 23 '17 at 15:45

3 Answers3

2

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 NOT including which you want to get your characters, so variable MM should be 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!  
Prithvish Baidya
  • 639
  • 1
  • 8
  • 19
  • Thanks for the help! I've fixed it now, i definitely had my inputerror = false in the wrong place haha – Emily Aug 23 '17 at 16:10
1

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.

Izaak van Dongen
  • 2,450
  • 13
  • 23
0

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.

Lex Scarisbrick
  • 1,540
  • 1
  • 24
  • 31
  • 1
    I think this will pass on an input like `20`, leading to a failure later on, but from the message `Numbers in format DDMMYY only`, you wouldn't expect it to. This was really more of a problem in OP's code than a problem with your answer to OP's question, though. – Izaak van Dongen Aug 23 '17 at 16:20