Python 如何将月份名称更改为数字?
这个月我开始学习python。我是一个初学者,所以我在将月份名称改为数字时遇到了问题。我尝试使用if命令,但它不起作用 我使用的代码是:Python 如何将月份名称更改为数字?,python,Python,这个月我开始学习python。我是一个初学者,所以我在将月份名称改为数字时遇到了问题。我尝试使用if命令,但它不起作用 我使用的代码是: d = int(input()) m = input() y = int(input()) a = y - 1900 if m == january: x = 1 print(f'It is {d} days, {x} months and {a} years at {d m y}.') 但是它不起作用,程序告诉我一月的名字没有定义。如何将月份名
d = int(input())
m = input()
y = int(input())
a = y - 1900
if m == january:
x = 1
print(f'It is {d} days, {x} months and {a} years at {d m y}.')
但是它不起作用,程序告诉我一月的名字没有定义。如何将月份名称更改为数字?您的一月不在引号中,因此Python将其作为变量读取。它应该是m='January'才能工作。您的一月不在引号中,因此Python将其作为变量读取。它应该是m==“一月”才能工作。您应该使用strtime模块 例如:
from time import strptime
input_month = 'January'
input_month = input_month[:3]
strptime(input_month,'%b').tm_mon
您应该使用strtime模块 例如:
from time import strptime
input_month = 'January'
input_month = input_month[:3]
strptime(input_month,'%b').tm_mon
如果m.lower==一月:
一月是一个变量,但您需要一个字符串:一月
要使用户输入不区分大小写,请使用m.lower:将所有输入字符转换为小写
若要说明所有月份,并在无效时触发用户的有效输入,您可以简单地执行以下操作:
months_indices = {"january":1, "february":2, "march":3, "april":4, "may":5, "june":6,
"july":7, "august":8, "september":9, "october":10, "november":11, "december":12}
while m.lower() not in months_indices:
m = input("Please enter a valid month")
x = months_indices[m.lower()] # index into a dictionary to return one of 1,2,...,12
如果m.lower==一月:
一月是一个变量,但您需要一个字符串:一月
要使用户输入不区分大小写,请使用m.lower:将所有输入字符转换为小写
若要说明所有月份,并在无效时触发用户的有效输入,您可以简单地执行以下操作:
months_indices = {"january":1, "february":2, "march":3, "april":4, "may":5, "june":6,
"july":7, "august":8, "september":9, "october":10, "november":11, "december":12}
while m.lower() not in months_indices:
m = input("Please enter a valid month")
x = months_indices[m.lower()] # index into a dictionary to return one of 1,2,...,12
程序显示名称“一月”未定义
为了解释,Python查看您的程序,需要知道哪些是变量,哪些是字符串。当您使用不带引号的一月时,Python解释器认为您希望使用一月作为变量。它查找一月变量,但找不到,因为它不存在
你需要的可能是比较m.lower和字符串,一月。要做到这一点,您只需将一月用单引号或双引号括起来,Python接受这两个引号
该行将如下所示:
如果m==‘一月’:
希望有帮助
程序显示名称“一月”未定义
为了解释,Python查看您的程序,需要知道哪些是变量,哪些是字符串。当您使用不带引号的一月时,Python解释器认为您希望使用一月作为变量。它查找一月变量,但找不到,因为它不存在
你需要的可能是比较m.lower和字符串,一月。要做到这一点,您只需将一月用单引号或双引号括起来,Python接受这两个引号
该行将如下所示:
如果m==‘一月’:
希望有帮助 最后一段删除括号后的“f”最后一段删除括号后的“f”我也认为字典是个好主意:
d = int(input('Please enter an integer for days:\t'))
m = input('Please enter the lowercase name of the month:\t')
y = int(input('Please enter an integer for the year:\t'))
a = y - 1900
months = {'january': 1, 'february': 2}
for index, item in enumerate(months):
if m == item.lower():
print(f'It is {d} days, {months[item]} months and {a} years at {d} {m} {y} .')
break
我也认为字典是个好主意:
d = int(input('Please enter an integer for days:\t'))
m = input('Please enter the lowercase name of the month:\t')
y = int(input('Please enter an integer for the year:\t'))
a = y - 1900
months = {'january': 1, 'february': 2}
for index, item in enumerate(months):
if m == item.lower():
print(f'It is {d} days, {months[item]} months and {a} years at {d} {m} {y} .')
break
你必须做m='january',否则你指的是变量JanuaryKindle,请按照下面的链接来回答类似的问题:请按照下面的链接来回答类似的问题:你必须做m='january',否则,您指的是变量JanuaryKindle,请按照下面的链接来回答类似的问题:请按照下面的链接来回答类似的问题:这是一个f字串,请参考了解详细信息。这是一个f字串,请参考了解详细信息。