缩写月份单词

类型:字符串

描述

月份的英文单词及其缩写如下所示:

  • 一月 Jan. January
  • 二月 Feb. February
  • 三月 Mar. March
  • 四月 Apr. April
  • 五月 May. May
  • 六月 Jun. June
  • 七月 Jul. July
  • 八月 Aug. August
  • 九月 Sept. September
  • 十月 Oct. October
  • 十一月 Nov. November
  • 十二月 Dec. December

月份的缩写为月份单词的前3个字母(9月为前4个),且首字母大写,以 '.' 做为缩写结束标记。编写一个程序,用户输入一个月份单词,不论输入的单词各字符是大写还是小写,请正确输出对应月份的缩写。当输入单词拼写错误时,输出“spelling mistake”。

# 提示,字符串有以下方法可用
str.upper()     转换字符串 str 中所有字母为大写
str.lower()      转换字符串 str 中所有字母为小写
str.capitalize()       把字符串 str 的第一个字符大写

# 月份名列表
month_lst = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

输入格式

某月份的英文单词

输出格式

该月的缩写或“spelling mistake”

示例 1

输入:february    
输出:Feb.

示例 2

输入:auGust       
输出:Aug.

参考代码

处理输入字符串,首先lower()转换为小写,然后capitalize()转化为首字母大写,之后就是分支判断了。

# 月份的英文单词及其缩写如下所示:
#
# 一月 Jan. January
# 二月 Feb. February
# 三月 Mar. March
# 四月 Apr. April
# 五月 May. May
# 六月 Jun. June
# 七月 Jul. July
# 八月 Aug. August
# 九月 Sept. September
# 十月 Oct. October
# 十一月 Nov. November
# 十二月 Dec. December
# 月份的缩写为月份单词的前3个字母,且首字母大写,以 '.' 做为缩写结束标记。
# 编写一个程序,用户输入一个月份单词,不论输入的单词各字符是大写还是小写,请正确输出对应月份的缩写。
# 当输入单词拼写错误时,输出“spelling mistake”。“拼写错误”

# str.upper() 转换字符串str中所有字母为大写
# # str.lower()       转换字符串str中所有字母为小写
# # str.capitalize()        把字符串str的第一个字符大写
# 分支语句实现
month = input().lower().capitalize()
if month == 'January':
         output = month[:3] + '.'
elif month == 'February':
         output = month[:3] + '.'
elif month == 'March':
         output = month[:3] + '.'
elif month == 'April':
         output = month[:3] + '.'
elif month == 'May':
         output = month[:3] + '.'
elif month == 'July':
         output = month[:3] + '.'
elif month == 'July':
         output = month[:3] + '.'
elif month == 'August':
         output = month[:3] + '.'
elif month == 'September':
         output = month[:4] + '.'
elif month == 'October':
         output = month[:3] + '.'
elif month == 'November':
         output = month[:3] + '.'
elif month == 'December':
    output = month[:3] + '.'
else:
    output = 'spelling mistake'
print(output)

# 逻辑表达式实现
month = input().lower().capitalize()
if month ==  'September':
    output = month[:4] + '.'
elif month == 'January' or month == 'February' or month == 'March' or month == 'April' or month == 'May' or \
                   month == 'July' or month == 'July' or month == 'August' or month == 'September' or month == 'October' or \
                   month == 'November' or month == 'December':
    output = month[:3] + '.'
else:
    output = 'spelling mistake'
print(output)

# 列表实现
month = input().lower().capitalize()
month_lst = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
             'November', 'December']
if month == 'September':
    print(month[:4] + '.')
elif month in month_lst:
    print(month[:3] + '.')
else:
    print('spelling mistake')
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐