鱼C论坛

 找回密码
 立即注册
查看: 1047|回复: 6

[已解决]python基础教程的源代码,在我的机子上不好跑,求看一下?

[复制链接]
发表于 2017-8-4 22:45:43 | 显示全部楼层 |阅读模式

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

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

x
  1. # Print out a date, given year, month, and day as numbers

  2. months = [
  3.     'January',
  4.     'February',
  5.     'March',
  6.     'April',
  7.     'May',
  8.     'June',
  9.     'July',
  10.     'August',
  11.     'September',
  12.     'October',
  13.     'November',
  14.     'December'
  15. ]

  16. # A list with one ending for each number from 1 to 31
  17. endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
  18.         + ['st', 'nd', 'rd'] +  7 * ['th'] \
  19.         + ['st']

  20. year    = input('Year: ')
  21. month   = input('Month  (1-12): ')
  22. day     = input('Day (1-31): ')

  23. month_number = int(month)
  24. day_number = int(day)

  25. # Remember to subtract 1 from month and day to get a correct index
  26. month_name = months[month_number-1]
  27. ordinal = day + endings[day_number-1]

  28. print(month_name + ' ' + ordinal + ', ' + year)
复制代码



  1. RESTART: /Users/Yuan_YangXin/Desktop/beginning-python-3ed-master/Chapter02/listing2-1.py
  2. Year: 1974
  3. Month  (1-12): 8
  4. Day (1-31): 16

  5. Traceback (most recent call last):
  6.   File "/Users/Yuan_YangXin/Desktop/beginning-python-3ed-master/Chapter02/listing2-1.py", line 32, in <module>
  7.     ordinal = day + endings[day_number-1]
  8. TypeError: unsupported operand type(s) for +: 'int' and 'str'
复制代码
最佳答案
2017-8-4 23:22:25
yuanyangxin 发表于 2017-8-4 23:04
[code]Traceback (most recent call last):
  File "/Users/Yuan_YangXin/Desktop/beginning-python-3ed ...

你用的是python2?
如果是的话,把input改为raw_input
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-8-4 22:46:46 | 显示全部楼层
另外,
  1. endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
  2.         + ['st', 'nd', 'rd'] +  7 * ['th'] \
  3.         + ['st']
复制代码

这句代码是干什么用的啊?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-4 22:56:11 | 显示全部楼层
第32行
ordinal = str(day) + endings[day_number-1]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-4 22:57:30 | 显示全部楼层
yuanyangxin 发表于 2017-8-4 22:46
另外,
这句代码是干什么用的啊?

从1日到31日,每天序数的结尾,如1日是st,2日是nd
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-4 23:04:15 | 显示全部楼层
冬雪雪冬 发表于 2017-8-4 22:56
第32行
ordinal = str(day) + endings[day_number-1]

[code]Traceback (most recent call last):
  File "/Users/Yuan_YangXin/Desktop/beginning-python-3ed-master/Chapter02/listing2-1.py", line 34, in <module>
    print(month_name + ' ' + ordinal + ', ' + year)
TypeError: cannot concatenate 'str' and 'int' objects
[/code


我这儿还是报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-4 23:22:25 | 显示全部楼层    本楼为最佳答案   
yuanyangxin 发表于 2017-8-4 23:04
[code]Traceback (most recent call last):
  File "/Users/Yuan_YangXin/Desktop/beginning-python-3ed ...

你用的是python2?
如果是的话,把input改为raw_input
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-5 03:17:51 | 显示全部楼层
从下面的错误原因就可以看到是类型没有进行转换导致的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 20:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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