将数值转换为 DOB 的代码的值错误
Value Error for code that converts numerical values into DOB
我试图创建一个程序,要求用户以数字格式提供他们的 DOB。它一直在工作,直到我指定如果他们输入的日期值超过 31,那么它是无效的。我收到此错误:ValueError: invalid literal for int() with base 10:
(天值)
我的代码如下所示:
uy = input("Enter your birth year: ")
um = input("Enter the number of your birth month (1-12): ")
mn = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
if um == "1":
mn = "January"
elif um == "2":
mn = "February"
elif um == "3":
mn = "March"
elif um == "4":
mn = "April"
elif um == "5":
mn = "May"
elif um == "6":
mn = "June"
elif um == "7":
mn = "July"
elif um == "8":
mn = "August"
elif um == "9":
mn = "September"
elif um == "10":
mn = "October"
elif um == "11":
mn = "November"
elif um == "12":
mn = "December"
day = input("please enter the day number of the date (1-31): ")
if day == '1' or day == '21' or day == '31':
day = day + "st"
elif day == '2' or day == '22':
day = day + "nd"
elif day == '3' or day == '23':
day = day + "rd"
else:
day = day + "th"
if mn == "February" and int(day) <= 28:
vd = True
elif (mn == "April" or mn == "June" or mn == "September" or mn == "November") and int(day) <= 30:
vd = True
elif mn in ["January", "March", "May", "July", "August", "October", "December"] and int(day) <= 31:
vd = True
else:
vd = False
if vd:
print(f"Your date of birth is the {day} {mn} {uy}")
else:
print("The data entered was not valid, please try again.")
我还是个初学者,我不知道自己做错了什么。感谢您的帮助。
您的问题是您修改了日期变量,添加了“nd”、“st”等。所以 day 变量现在是一个数字和字母。该程序不知道如何转换为 int 并崩溃。我建议使用另一个变量 day_modified 来存储 31st.
的值
我试图创建一个程序,要求用户以数字格式提供他们的 DOB。它一直在工作,直到我指定如果他们输入的日期值超过 31,那么它是无效的。我收到此错误:ValueError: invalid literal for int() with base 10:
(天值)
我的代码如下所示:
uy = input("Enter your birth year: ")
um = input("Enter the number of your birth month (1-12): ")
mn = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
if um == "1":
mn = "January"
elif um == "2":
mn = "February"
elif um == "3":
mn = "March"
elif um == "4":
mn = "April"
elif um == "5":
mn = "May"
elif um == "6":
mn = "June"
elif um == "7":
mn = "July"
elif um == "8":
mn = "August"
elif um == "9":
mn = "September"
elif um == "10":
mn = "October"
elif um == "11":
mn = "November"
elif um == "12":
mn = "December"
day = input("please enter the day number of the date (1-31): ")
if day == '1' or day == '21' or day == '31':
day = day + "st"
elif day == '2' or day == '22':
day = day + "nd"
elif day == '3' or day == '23':
day = day + "rd"
else:
day = day + "th"
if mn == "February" and int(day) <= 28:
vd = True
elif (mn == "April" or mn == "June" or mn == "September" or mn == "November") and int(day) <= 30:
vd = True
elif mn in ["January", "March", "May", "July", "August", "October", "December"] and int(day) <= 31:
vd = True
else:
vd = False
if vd:
print(f"Your date of birth is the {day} {mn} {uy}")
else:
print("The data entered was not valid, please try again.")
我还是个初学者,我不知道自己做错了什么。感谢您的帮助。
您的问题是您修改了日期变量,添加了“nd”、“st”等。所以 day 变量现在是一个数字和字母。该程序不知道如何转换为 int 并崩溃。我建议使用另一个变量 day_modified 来存储 31st.
的值