zoukankan      html  css  js  c++  java
  • python: "TypeError: 'type' object is not subscriptable"

    目前stackoverflow找到两种情况的解决办法:

    1、TypeError: 'type' object is not subscriptable when indexing in to a dictionary

    I have multiple files that I need to load so I'm using a dict to shorten things. When I run I get a "TypeError: 'type' object is not subscriptable" Error. How can I get this to work?

    m1 = pygame.image.load(dict[1])
    m2 = pygame.image.load(dict[2])
    m3 = pygame.image.load(dict[3])
    dict = {1: "walk1.png", 2: "walk2.png", 3: "walk3.png"}
    playerxy = (375,130)
    window.blit(m1, (playerxy))
    

    Normally Python throws NameError if the variable is not defined:

    >>> d[0]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'd' is not defined
    

    However, you've managed to stumble upon a name that already exists in Python.

    Because dict is the name of a built-in type in Python you are seeing what appears to be a strange error message, but in reality it is not.

    The type of dict is a type. All types are objects in Python. Thus you are actually trying to index into the type object. This is why the error message says that the "'type' object is not subscriptable."

    >>> type(dict)
    <type 'type'>
    >>> dict[0]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'type' object is not subscriptable
    

    Note that you can blindly assign to the dict name, but you really don't want to do that. It's just going to cause you problems later.

    >>> dict = {1:'a'}
    >>> type(dict)
    <class 'dict'>
    >>> dict[1]
    'a'
    

    The true source of the problem is that you must assign variables prior to trying to use them. If you simply reorder the statements of your question, it will almost certainly work:

    d = {1: "walk1.png", 2: "walk2.png", 3: "walk3.png"}
    m1 = pygame.image.load(d[1])
    m2 = pygame.image.load(d[2])
    m3 = pygame.image.load(d[3])
    playerxy = (375,130)
    window.blit(m1, (playerxy))


    2、TypeError: 'int' object is not subscriptable

    I'm trying to create a simple program that tells you your lucky number according to numerology. I keep on getting this error:

    File "number.py", line 12, in <module>
        sumln = (int(sumall[0])+int(sumall[1]))
    TypeError: 'int' object is not subscriptable
    

    My script is:

    birthday = raw_input("When is your birthday(mm/dd/yyyy)? ")
    summ = (int(birthday[0])+int(birthday[1]))
    sumd = (int(birthday[3])+int(birthday[4]))
    sumy= (int(birthday[6])+int(birthday[7])+int(birthday[8])+int(birthday[9]))
    sumall = summ + sumd + sumy
    print "The sum of your numbers is", sumall
    sumln = (int(sumall[0])+int(sumall[1]))
    print "Your lucky number is", sumln`   
    

    If you want to sum the digit of a number, one way to do it is using sum() + a generator expression:

    sum(int(i) for i in str(155))
    

    I modified a little your code using sum(), maybe you want to take a look at it:

    birthday = raw_input("When is your birthday(mm/dd/yyyy)? ")
    summ = sum(int(i) for i in birthday[0:2])
    sumd = sum(int(i) for i in birthday[3:5])
    sumy = sum(int(i) for i in birthday[6:10])
    sumall = summ + sumd + sumy
    print "The sum of your numbers is", sumall
    sumln = sum(int(c) for c in str(sumall)))
    print "Your lucky number is", sumln
    
  • 相关阅读:
    Linux下Tomcat日志分割
    adb logcat 命令使用说明
    linux系统下安装两个或多个tomcat
    架构师小跟班:SSL证书免费申请及部署,解决页面样式错乱问题完整攻略
    springboot获取七牛云空间文件列表及下载功能
    Java使用ganymed工具包执行LINUX命令教程
    Java学生信息管理系统源码
    数据库SQL语句性能优化
    Java开发环境系列:一篇能解决你99%问题的排雷日记
    架构师小跟班:教你从零开始申请和配置七牛云免费OSS对象存储(不能再详细了)
  • 原文地址:https://www.cnblogs.com/noxy/p/7903793.html
Copyright © 2011-2022 走看看