简体   繁体   English

Python 程序在不应该打印时打印 None

[英]Python program prints None when it isn't supposed to

I wrote a program that outputs the longest sequence of equal numbers when I press 2 in the console, but for some reason it prints the numbers tied to each other (ie if I input 1 2 2 2 4 5 it outputs 222 instead of 2 2 2) and for some reason it also prints None我编写了一个程序,当我在控制台中按 2 时,它会输出最长的相等数字序列,但由于某种原因,它会打印出相互关联的数字(即,如果我输入 1 2 2 2 4 5,它会输出 222 而不是 2 2 2)由于某种原因它也打印无

The assertion tests that I made also fail for some reason我所做的断言测试也由于某种原因失败了

Input: press 1, input 1 2 2 2 2 4 5 6 1 4输入:按1,输入1 2 2 2 2 4 5 6 1 4

Expected output: press 2, then it should show 2 2 2 2 2预期输出:按 2,则应显示 2 2 2 2 2

Actual output: 22222 None实际输出:22222 无

def lista_egale(lst1):
    l = 1
    prev_one = None
    number = 0
    lmax = -1
    for current in lst1:
        if prev_one == current:
            l += 1
        elif l > lmax:
            lmax = l
            number = prev_one
            l = 1
        prev_one = current
    print(number * lmax)

def test_egale():
    assert lista_egale([1, 2, 2, 2, 4, 5, 6]) == 2 2 2
    assert lista_egale([4, 1, 2, 4, 1, 2, 3, 2]) == 1
    assert lista_egale([1, 1, 1, 1, 1, 2, 2, 2, 2]) == 1 1 1 1 1

def show_menu():
    print("lists")
    print("1. Enter the list")
    print("2. Check if the list has a max sequence of equal numbers and print it")
    print("4. exit")


def ui_read_list():
     input_list = input("Numbers go here ")
     return input_list.split()

def run():
    global lst1
    lst1 = []
    show_menu()
    while True:
        cmd = input(">>>")
        if cmd == "4":
            return
        if cmd == "1":
            lst1 = ui_read_list()
        elif cmd == "2":
            print(lista_egale(lst1))
        else:
            print("invalid command")

def main():
    run()
    test_egale()
    #test_munte()

main()

you run你跑

print(lista_egale(lst1))

The return value of your function函数的返回值

lista_egale(lst1)

is None , which gets printed.None ,它被打印出来。 Probably you want to run simply:可能你想简单地运行:

lista_egale(lst1)

This worked for me except for the second assertion which (to me) doesn't make sense at all.这对我有用,除了第二个断言(对我来说)根本没有意义。 Why would you want [1] to be returned and not anything else?为什么要返回 [1] 而不是其他任何东西? Anyway, it returns [4] in that case.无论如何,在这种情况下它返回 [4]。

def lista_egale(lst1):
    l = 0
    prev_one = None
    number = 0
    lmax = -1
    for current in lst1:
        if prev_one == current:
            l += 1
        elif l > lmax:
            lmax = l
            number = prev_one
            l = 1
        prev_one = current
    return ([number] * lmax)

def test_egale():
    assert lista_egale([1, 2, 2, 2, 4, 5, 6]) == [2, 2, 2]
    assert lista_egale([4, 1, 2, 4, 1, 2, 3, 2]) == [1]
    assert lista_egale([1, 1, 1, 1, 1, 2, 2, 2, 2]) == [1, 1, 1, 1, 1]

def show_menu():
    print("lists")
    print("1. Enter the list")
    print("2. Check if the list has a max sequence of equal numbers and print it")
    print("4. exit")


def ui_read_list():
    input_list = input("Numbers go here ")
    return input_list.split()

def run():
    global lst1
    lst1 = []
    show_menu()
    while True:
        cmd = input(">>>")
        if cmd == "4":
            return
        if cmd == "1":
            lst1 = ui_read_list()
        elif cmd == "2":
            print(lista_egale(lst1))
        else:
            print("invalid command")

def main():
    run()
    test_egale()
    #test_munte()

main()

EDIT:编辑:

Your mistakes were:你的错误是:

  1. l was initialized as 1 even though it should've been initialized to 0 (because if the longest "streak" is 1-long, nothing would reach the l>lmax . l被初始化为 1,即使它应该被初始化为 0(因为如果最长的“连续”是 1-long,则没有任何东西会达到l>lmax
  2. In the lista_eagle function you didn't return anything and instead printed the result.lista_eagle函数中,您没有返回任何内容,而是打印了结果。
  3. You were printing number * lmax which is a number instead of the streak itself which you can get by concatenating the array to itiself lmax times (as shown here at return [number] * lmax ).您正在打印number * lmax ,它是一个数字而不是条纹本身,您可以通过将数组连接到自身 lmax 次来获得它(如return [number] * lmax )。

As others have pointed out, it sounds like you're expecting lista_egale to return a value rather than printing it.正如其他人指出的那样,听起来您希望lista_egale返回一个值而不是打印它。 I think you also want to use the count function of lists to find how many times each item is in the list like this:我认为您还想使用列表的count功能来查找每个项目在列表中的次数,如下所示:

def lista_egale(lst1):
    lmax = -1
    number = 0
    for current in lst1:
        if lst1.count(current) > lmax:
            lmax = lst1.count(current)
            number = current
    return ' '.join([str(number)] * lmax)

Then to make your asserts work, the comparisons should also be strings like this:然后为了让你的断言起作用,比较也应该是这样的字符串:

def test_egale():
    assert lista_egale([1, 2, 2, 2, 4, 5, 6]) == '2 2 2'
    assert lista_egale([4, 1, 2, 4, 1, 2, 3, 2]) == '1'
    assert lista_egale([1, 1, 1, 1, 1, 2, 2, 2, 2]) == '1 1 1 1 1'

In lista_egale() 's print staement do this:lista_egale()的 print staement 中执行以下操作:

print((str(number)+" ")*lmax)

This will print 2 2 2 rather than 222这将打印 2 2 2 而不是 222

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM