-5

I'm trying to convert date (i.e. 6/15/2000) to the day, month abbreviation, and year (i.e. 15 June, 2000) in python 3

def main():
    dateStr = raw_input("Enter a date (mm/dd/yyyy): ")
    monthStr, dayStr, yearStr = string.split(dateStr, "/")
    months = ["January", "February", "March", "April", "May", "June", 
    "July", "August", "September", "October", "November", "December"]
    monthStr = months[int(monthStr)–1]   

main()
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • https://stackoverflow.com/questions/954834/how-do-i-use-raw-input-in-python-3 – cs95 Feb 20 '18 at 02:32
  • Which Python tutorial are you following? Which version of Python is that tutorial designed for? Does that match the version of Python you are using? – Greg Hewgill Feb 20 '18 at 02:36
  • 3
    **Tell us what the exact wording of the error message is, and which line of code is producing it.** https://stackoverflow.com/help/mcve – Josh Lee Feb 20 '18 at 02:37
  • 1
    Your `–` in `int(monthStr)–1` is not the minus sign but a _dash_, replace with a `-`. if you posted the error it would be easier to help. (Also `raw_input()` is not in Python 3) – AChampion Feb 20 '18 at 02:37

1 Answers1

0

Python 3 Code:

def main():
    dateStr = input("Enter a date (mm/dd/yyyy): ")
    monthStr, dayStr, yearStr = dateStr.split("/")
    months = ["January", "February", "March", "April", "May", "June", 
    "July", "August", "September", "October", "November", "December"]
    monthStr = months[int(monthStr)-1]
    print ('Converted Date: ' + ' '.join([str(dayStr), monthStr, str(yearStr)]))

main()

Python 2 Code: Use raw_input:

def main():
    dateStr = raw_input("Enter a date (mm/dd/yyyy): ")
    monthStr, dayStr, yearStr = dateStr.split("/")
    months = ["January", "February", "March", "April", "May", "June", 
    "July", "August", "September", "October", "November", "December"]
    monthStr = months[int(monthStr)-1]
    print ('Converted Date: ' + ' '.join([str(dayStr), monthStr, str(yearStr)]))

main()

Explanation

First, your split syntax seems to be incorrect. it should be <str>.split(<split character>) i.e. dateStr.split("/")

Next, in Python 3, there is no raw_input as it is renamed as input().

Lastly, I added the print statement.

Hope it helps.

Gareth Ma
  • 329
  • 2
  • 11