Here is my code: The error claims that my int(user month) -1 must be a string or number. But im confused because i have the number in my list [0]

def change_date_format(user_date):
    user_date_values = user_date.split("/")

    user_month = user_date_values[0]
    user_day = user_date_values[1]
    user_year = user_date_values[2]

    return user_date_values, user_day, user_year
def main():
    user_date = input("Enter a date in the form mm/dd/yyyy: ")

    months = ["January", "Febuary", "March", \
              "April", "May", "June",\
              "July", "August", "September", \
              "October", "November", "December"]
    user_month, user_day, user_year = change_date_format(user_date)

    user_month_index = int(user_month) - 1
    user_month_name = months[user_month_index]

    new_date_format = user_month_name + " " + user_day + ", " + \
                      user_year

    print("You typed " + user_date, "The format of your date was changed \
to " + new_date_format, sep = "\n")

main()

2 Answers

0
Burhan Khalid On

In your function change_date_format, instead of returning user_month, you are returning user_date_values, which is a list.

Try this:

def change_date_format(user_date):
    user_date_values = user_date.split("/")

    user_month = user_date_values[0]
    user_day = user_date_values[1]
    user_year = user_date_values[2]

    return user_month, user_day, user_year
1
Devesh Kumar Singh On

You need to return user_month in your function change_date_format, instead of user_date_values which is a list

def change_date_format(user_date):
    user_date_values = user_date.split("/")

    user_month = user_date_values[0]
    user_day = user_date_values[1]
    user_year = user_date_values[2]

    return user_month, user_day, user_year

Your expected output will then be.

Enter a date in the form mm/dd/yyyy: 02/02/2002
You typed 02/02/2002
The format of your date was changed to Febuary 02, 2002

You can also return the list itself as below, and the variables will be assigned accordingly.

def change_date_format(user_date):
    user_date_values = user_date.split("/")
    return user_date_values

The format above will result in the same thing, as the elements in the list will be assigned to the variables like below

user_month, user_day, user_year = change_date_format(user_date)