python123题目——缩写月份单词

缩写月份单词

类型:字符串

描述

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

  • 一月 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')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_62488776

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值