鱼C论坛

 找回密码
 立即注册
查看: 1101|回复: 3

[已解决]求助看一下参考书中代码

[复制链接]
发表于 2021-3-3 17:10:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
以下是书里的例子
# 将以数指定年、月、日的日期打印出来
months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
# 一个列表,其中包含数1~31对应的结尾
endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
+ ['st', 'nd', 'rd'] + 7 * ['th'] \
+ ['st']
year = input('Year: ')
month = input('Month (1-12): ')
day = input('Day (1-31): ')
month_number = int(month)
day_number = int(day)
# 别忘了将表示月和日的数减1,这样才能得到正确的索引
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]
print(month_name + ' ' + ordinal + ', ' + year)
这个程序的运行情况类似于下面这样:
Year: 1974
Month (1-12): 8
Day (1-31): 16
August 16th, 1974


但是我发现如果将
endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
+ ['st', 'nd', 'rd'] + 7 * ['th'] \
+ ['st']

和ordinal = day + endings[day_number-1]这里加号后面的都删掉
程序并没有什么问题,那么这段endings的代码为什么要这样写?这些st nd rd又是什么意思?
请求大佬解答一下
最佳答案
2021-3-3 17:21:56
本帖最后由 情绪z 于 2021-3-3 17:27 编辑

https://zhidao.baidu.com/question/1861749381219668627.html你可以打印一下endings,它是一个列表,把加号后面的东西删掉打印的结果是不一样的
  1. # 一个列表,其中包含数1~31对应的结尾,
  2. # 17 * ['th'] 是把['th']重复17次,因为4-20是加th
  3. endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
  4.           + ['st', 'nd', 'rd'] + 7 * ['th'] \
  5.           + ['st']
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-3 17:21:56 | 显示全部楼层    本楼为最佳答案   
本帖最后由 情绪z 于 2021-3-3 17:27 编辑

https://zhidao.baidu.com/question/1861749381219668627.html你可以打印一下endings,它是一个列表,把加号后面的东西删掉打印的结果是不一样的
  1. # 一个列表,其中包含数1~31对应的结尾,
  2. # 17 * ['th'] 是把['th']重复17次,因为4-20是加th
  3. endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
  4.           + ['st', 'nd', 'rd'] + 7 * ['th'] \
  5.           + ['st']
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-3 18:24:55 | 显示全部楼层
     把数字变成序数,例如, '1'  -> '1st' ,'2'  -> '2nd' , '3'  ->  '3rd'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-3 18:43:49 | 显示全部楼层
情绪z 发表于 2021-3-3 17:21
https://zhidao.baidu.com/question/1861749381219668627.html你可以打印一下endings,它是一个列表,把加 ...

谢谢,解答的很详细
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 16:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表