Python语言输出月份对应的花名
时间: 2023-11-05 17:08:38 浏览: 56
可以使用Python的字典(dictionary)来实现月份与花名的对应关系,具体代码如下:
```python
flowers = {
"January": "Carnation",
"February": "Violet",
"March": "Daffodil",
"April": "Daisy",
"May": "Lily of the Valley",
"June": "Rose",
"July": "Larkspur",
"August": "Gladiolus",
"September": "Aster",
"October": "Marigold",
"November": "Chrysanthemum",
"December": "Poinsettia"
}
month = input("请输入月份:")
flower = flowers.get(month.capitalize())
if flower:
print("{}月的花是{}".format(month, flower))
else:
print("输入有误,请输入正确的月份")
```
上述代码将用户输入的月份与字典中的键(月份)进行比较,如果存在对应的值(花名),则输出该月份和对应的花名;否则提示用户输入有误。
相关问题
用python语言输入一个月份数字(1-12),输出对应月份的名称缩写
好的,针对您的问题,可以使用以下Python代码来实现:
```python
month = int(input("请输入月份数字(1-12):"))
if month == 1:
print("Jan")
elif month == 2:
print("Feb")
elif month == 3:
print("Mar")
elif month == 4:
print("Apr")
elif month == 5:
print("May")
elif month == 6:
print("Jun")
elif month == 7:
print("Jul")
elif month == 8:
print("Aug")
elif month == 9:
print("Sep")
elif month == 10:
print("Oct")
elif month == 11:
print("Nov")
elif month == 12:
print("Dec")
else:
print("输入有误,月份数字应在1-12之间!")
```
利用if-elif-else语句,根据用户输入的月份数字进行判断,输出对应的月份名称缩写。
希望能够帮助到您,如有疑问请随时提出。
用python语言实现输入年份和月份,输出该月的天数
以下是Python代码实现:
```
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
# 计算天数
if month in [1, 3, 5, 7, 8, 10, 12]:
days = 31
elif month in [4, 6, 9, 11]:
days = 30
elif month == 2:
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
days = 29
else:
days = 28
else:
print("输入的月份有误!")
# 输出结果
if month in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
print("{}年0{}月有{}天".format(year, month, days))
else:
print("{}年{}月有{}天".format(year, month, days))
```
运行程序后,会提示输入年份和月份,然后输出该月的天数。其中,计算天数的部分使用了if语句和逻辑运算符,判断该月份的天数是31天、30天还是28/29天。最后,根据月份的位数输出对应的结果。