python123题目——缩写月份单词

阅读: 评论:0

python123题目——缩写月份单词

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')

本文发布于:2024-01-29 09:27:52,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170649167714314.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:缩写   单词   题目
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23