【问题标题】:(python) print keep saying 'None'(python)打印一直说“无”
【发布时间】:2020-04-15 03:55:26
【问题描述】:

我正在为项目制作一些代码,我想要在测试功能中做的就是我想要打印出来 我输入的每个“list_total[y]”值

输入示例

1 # forget this input for now, 

1 # the lines how many input i want to

100, 200 , 300 # i used a map 

那么它应该被打印出100, 200 ,300 而是'None'

我的目标是在每个 'list_total[y]' 中绘制最高值,但似乎返回值是 'none'

所以我也收到一条错误消息

'TypeError: 'NoneType' 对象不可迭代'

当我编写代码时

like  return print(max(list_total[y]))

这是我在下面制作的代码

T = eval(input(": "))


list_total = []


def test(value):

    for x in range(value):
            n = eval(input(": "))
            for y in range(n):
                list_total[y] = list_total.append(list(map(int, input(": ").split())))
                return print(list_total[y])  # make sure code is working




test(T)

【问题讨论】:

  • 请重新格式化问题。我认为有人的编辑已经在等待您的批准,

标签: python-extensions


【解决方案1】:

如果你想在这里订购它们,你会怎么做:

T = eval(input(": "))


list_total = []


def test(value):

    for x in range(value):
        n = eval(input("enter the "+str(x)+"th number: "))
        list_total.append(n)

    print(list_total)         
    return list_total.sort() # make sure code is working

test(T)
print(list_total)

示例输出:

: 4
enter the 0th number: 4
enter the 1th number: 2
enter the 2th number: 1
enter the 3th number: 2
[4, 2, 1, 2]
[1, 2, 2, 4]

但是您想使用每个条目更新数组,在这种情况下,您可以使用一个变量来保存列表中的最大值,然后将其与新条目进行比较。

如果你还是想检查整个数组......你遇到的问题是:

  1. 您正在获取的输入中使用 map 方法。你在这里不需要那个。 input("helper text") 中的文本仅提示用户程序需要输入。您不必从输入中删除该帮助文本。

  2. 单独打印并返回

  3. 修正这一行:list_total[y] = list_total.append(list(map(int, input(": ").split())))

  4. 内部 for 循环不应运行到 range(n) 尝试 range(len(list_total))

  5. 如果您希望在任何给定时间仅最大元素出现在列表中:

list_total = list(max(list_total))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 2011-12-01
    相关资源
    最近更新 更多