【问题标题】:Why is there an odd error message when using the min() function?为什么使用 min() 函数时会出现奇怪的错误消息?
【发布时间】:2020-02-05 07:36:00
【问题描述】:

当我执行以下代码时,由于某种原因,我收到一条奇怪的错误消息。

我尝试将 int 更改为 float,但没有任何效果。

number = float(input("Please enter an integer number."))
if number % 1 == 0:
    list = []
    list.append(number)
    add = input("Would you like to add any more integers? Type Y for yes and N for no.")
    while add == "Y":
        newnumber = input("What is the next integer?")
        list.append([newnumber])
        add = input("Would you like to add any more integers? Type Y for yes and N for no.")
    if add == "N":
        print("Length: %s" % len(list))
        print("Minimum: %s" % int(min(list)))
else:
    print("This is not an integer. Please restart.")


这发生在第 12 行或 print("Minimum ...) 行。 'list' 和 'float' 的实例之间不支持 '

我们将不胜感激。

【问题讨论】:

    标签: python-3.x statistics min


    【解决方案1】:

    请将 list.append([newnumber]) 更改为 list.append(int(newnumber)) 因为您要附加一个 list([8.0, ['9'], ['6']]) 而不是另一个整数在列表中

    number = float(input("Please enter an integer number."))
    if number % 1 == 0:
        list = []
        list.append(number)
        add = input("Would you like to add any more integers? Type Y for yes and N for no.")
        while add == "Y":
            newnumber = input("What is the next integer?")
            list.append(int(newnumber))
            add = input("Would you like to add any more integers? Type Y for yes and N for no.")
        if add == "N":
            print("Length: %s" % len(list))
            print("Minimum: %s" % int(min(list)))
    else:
        print("This is not an integer. Please restart.")
    

    【讨论】:

    • 感谢您的帮助。
    猜你喜欢
    • 2021-05-30
    • 2019-10-28
    • 2021-10-15
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    相关资源
    最近更新 更多