[Solved] Syntax error in python 3 when I’m trying to convert a date but no luck on trying to solve it [duplicate]


Python 3 Code:

def main():
    dateStr = input("Enter a date (mm/dd/yyyy): ")
    monthStr, dayStr, yearStr = dateStr.split("https://stackoverflow.com/")
    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("https://stackoverflow.com/")
    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("https://stackoverflow.com/")

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

Lastly, I added the print statement.

Hope it helps.

2

solved Syntax error in python 3 when I’m trying to convert a date but no luck on trying to solve it [duplicate]