【问题标题】:Python program prints None when it isn't supposed toPython 程序在不应该打印时打印 None
【发布时间】:2021-12-01 21:32:37
【问题描述】:

我编写了一个程序,当我在控制台中按 2 时,它会输出最长的相等数字序列,但由于某种原因,它会打印相互关联的数字(即,如果我输入 1 2 2 2 4 5,它会输出 222 of 2 2 2) 并且由于某种原因它也打印 None

我所做的断言测试也由于某种原因失败了

输入:按1,输入1 2 2 2 2 4 5 6 1 4

预期输出:按 2,然后应该显示 2 2 2 2 2

实际输出: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 print(lista_egale(lst1))lista_egale 是一个不显式返回任何内容的函数,因此它将返回 None,然后将其打印出来。调用就行了,不打印返回值。

标签: python list spacing


【解决方案1】:

lista_egale() 的打印状态中执行以下操作:

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

这将打印 2 2 2 而不是 222

【讨论】:

    【解决方案2】:

    正如其他人所指出的,听起来您希望 lista_egale 返回一个值而不是打印它。我想您还想使用列表的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)
    

    那么为了让你的断言起作用,比较也应该是这样的字符串:

    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'
    

    【讨论】:

      【解决方案3】:

      这对我有用,除了第二个断言(对我来说)根本没有意义。为什么要返回 [1] 而不是其他任何东西?无论如何,在这种情况下它会返回 [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()
      

      编辑:

      你的错误是:

      1. l 被初始化为 1,即使它应该被初始化为 0(因为如果最长的“连续”是 1 长,则不会到达 l>lmax
      2. lista_eagle 函数中,您没有返回任何内容,而是打印了结果。
      3. 您正在打印 number * lmax,这是一个数字,而不是条纹本身,您可以通过将数组连接到 itiself lmax 次来获得它(如 return [number] * lmax 所示)。

      【讨论】:

        【解决方案4】:

        你跑

        print(lista_egale(lst1))
        

        你的函数的返回值

        lista_egale(lst1)
        

        None,它会被打印出来。可能你想简单地运行:

        lista_egale(lst1)
        

        【讨论】:

        • 是的,我在发布问题后不久就意识到了这一点并忘记更新它:/但是,我的断言仍然失败
        猜你喜欢
        • 2018-05-26
        • 1970-01-01
        • 1970-01-01
        • 2011-12-05
        • 2023-02-05
        • 1970-01-01
        • 2021-04-24
        • 1970-01-01
        相关资源
        最近更新 更多