正在编写一个将整数转换为月份名称的小脚本。我目前遇到的问题是我目前正在尝试熟悉字典,并存储了值为“January”的整数 1。当我在此字典上使用 get 方法时,它不会打印该值,而只是重新启动循环。这是代码:
months = {1: "January"}
while True:
try:
month_number_input = input("Enter a month:")
if month_number_input.isalpha():
raise ValueError("Please enter a number")
elif month_number_input == 1:
print(months.get(1))
except ValueError as VE:
print(VE)
最佳答案
更改您的代码,例如
elif int(month_number_input) == 1:
“month_number_input”不是 int 类型,因此您必须进行类型转换
关于python - 进行一个将整数转换为月份的小练习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57815258/