类型错误:“int"对象在等式中不可下标 [英] TypeError: 'int' object is not subscriptable in equation

查看:19
本文介绍了类型错误:“int"对象在等式中不可下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第四个 python 脚本,所以请忍受我的新手......我正在编写一个脚本,告诉某个日期的星期几.除了一个错误外,一切都运行良好.我对出了什么问题有一个模糊的想法,但不太确定:

This is my fourth python script, so please bear with my newbieness... I am writing a script that tells the day of week on a certain date. It all runs fine except one error. I have a faint idea as to what is wrong, but am not too certain:

TypeError: 'int' 对象不可下标

TypeError: 'int' object is not subscriptable

#!/usr/bin/python
import sys, string

# Call example:  day_of_week(2,10,1988); February 10, 1988
def day_of_week(month, date, year):
    # January
    if month == 1: m = 11
    # February
    elif month == 2: m = 12
    # March
    elif month == 3: m = 1
    # April
    elif month == 4: m = 2
    # May
    elif month == 5: m = 3
    # June
    elif month == 6: m = 4
    # July
    elif month == 7: m = 5
    # August
    elif month == 8: m = 6
    # September
    elif month == 9: m = 7
    # October
    elif month == 10: m = 8
    # November
    elif month == 11: m = 9
    # December
    elif month == 12: m = 10

    # Calculate the day of the week
    dow = (date + ((13 * m-1)/5) + year[2:] + (year[2:]/4) + (year[:2]/4) - 2 * year[:2]) % 7)

    # Formatting!
    if dow == 0: return "Sunday"
    elif dow == 1: return "Monday"
    elif dow == 2: return "Tuesday"
    elif dow == 3: return "Wednesday"
    elif dow == 4: return "Thursday"
    elif dow == 5: return "Friday"
    elif dow == 6: return "Saturday"
    else: return "Error!"

try:
    m = int(raw_input("What month were you born in (1-12)?  "))
    if not 1 <= m <= 12: raise Exception("There are no months with a number higher than 12!")
    d = int(raw_input("On what date were you born on?  "))
    y = int(raw_input("What year were you born in?  "))
    print("\nYou were born on a %s!" % day_of_week(m,d,y))
except ValueError:
    print("You need to enter a number!")

推荐答案

在这一行:

dow = (date + ((13 * m-1)/5) + year[2:] + (year[2:]/4) + (year[:2]/4) - 2 * year[:2]) % 7)

您正在使用 year(一个整数)并试图从中返回一个切片.正如错误所示,int 对象不允许您这样做.为了完成您想要的操作,您需要将 year 转换为字符串.

You are using year (an integer) and trying to return a slice from it. As the error indicates, int objects do not allow you to do this. In order to accomplish what you want, you would need to cast year as a string.

然而,更简单的解决方案可能是使用 datetime 模块的内置功能来计算星期几:

However an easier solution may be to use the built-in features of the datetime module to calculate the day of the week:

In [1]: import datetime

In [2]: my_date = datetime.date(2012, 11, 26)

In [3]: my_date.weekday()
Out[3]: 0

这使用星期一作为开始日期 (0).为了保持您当前的代码,您可以使用 isoweekday(),其中 Monday = 1:

This uses Monday as the starting date (0). To keep with your current code, you can use isoweekday(), where Monday = 1:

In [11]: import datetime

In [12]: my_date = datetime.date(2012, 11, 26)

In [13]: my_date.isoweekday()
Out[13]: 1

然后您可以使用上面@JonClement 的漂亮代码段来返回当天的字符串:

You can then use @JonClement's slick snippet above to return the string of the day:

In [14]: '{0:%A}'.format(my_date)
Out[14]: 'Monday'

这篇关于类型错误:“int"对象在等式中不可下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆