这是我的代码:
该错误表明我的int(user month) -1
必须是字符串或数字。但是我很困惑,因为我在列表[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()
答案 0 :(得分:1)
您需要在函数user_month
中返回change_date_format
,而不是列表中的user_date_values
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
您的预期输出将是。
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
您还可以如下所示返回列表本身,变量将得到相应分配。
def change_date_format(user_date):
user_date_values = user_date.split("/")
return user_date_values
上面的格式将产生相同的结果,因为列表中的元素将被分配给下面的变量
user_month, user_day, user_year = change_date_format(user_date)
答案 1 :(得分:0)
在函数change_date_format
中,您将返回列表中的user_month
,而不是返回user_date_values
。
尝试一下:
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