NoneType 对象不可下标 [英] NoneType object is not subscriptable

查看:106
本文介绍了NoneType 对象不可下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Learnstreet 上进行一个七段显示项目.所需的方法之一采用从标记为inputString"的文本框中获得的输入.然后使用作为字符串的 text 将输入字符串传递给给定的方法.该方法的进一步描述如下 -

I am working on a seven-segment display project on Learnstreet. One of the required methods takes the input obtained from a text box which is labelled "inputString". Input String is then passed to the given method using text which is a string. Further description of the method is as given below -

在这个方法中,我们从输入字符串中获取了文本,我们需要从中提取每个字符,如果是数字,则将其传递给我们的illumination函数以使用7段显示器显示该数字.

In this method we have got the text from that input string, and we need to extract each character from it, and if it is a number, pass it on to our illuminate function to display that number using a 7-segment display.

这是我解析字符串并从数字中提取数字的代码.

Here is my code for parsing the string and extracting digits from the number.

def get_digits(text):
    num=int(text)
    while num!=0:
        print illuminate(num%10)
        num/=10

此代码引发错误:

回溯(最近一次调用最后一次):文件",第 1 行,在文件",第 27 行,在测试文件",第 17 行,在 test_get_digits 中 TypeError: 'NoneType' object is not subscriptable.

Traceback (most recent call last): File "", line 1, in File "", line 27, in test File "", line 17, in test_get_digits TypeError: 'NoneType' object is not subscriptable.

因为我是 python 新手,我不明白是什么原因造成的.请帮忙

Because I am new to python, I don't understand what is causing this.Please help

推荐答案

其他要求之一是返回一些东西.由于您的函数不返回任何内容,None 被隐式返回,这会导致测试脚手架失败.也许您应该返回一个包含每次调用 illuminate() 的返回值的列表.

One of the other requirements is to return something. Since your function returns nothing, None is implicitly returned, and this causes the test scaffolding to fail. Perhaps you should return a list containing the return values of each invocation of illuminate().

这篇关于NoneType 对象不可下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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