[英]Python program prints None when it isn't supposed to
我編寫了一個程序,當我在控制台中按 2 時,它會輸出最長的相等數字序列,但由於某種原因,它會打印出相互關聯的數字(即,如果我輸入 1 2 2 2 4 5,它會輸出 222 而不是 2 2 2)由於某種原因它也打印無
我所做的斷言測試也由於某種原因失敗了
輸入:按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()
你跑
print(lista_egale(lst1))
函數的返回值
lista_egale(lst1)
是None
,它被打印出來。 可能你想簡單地運行:
lista_egale(lst1)
這對我有用,除了第二個斷言(對我來說)根本沒有意義。 為什么要返回 [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()
編輯:
你的錯誤是:
l
被初始化為 1,即使它應該被初始化為 0(因為如果最長的“連續”是 1-long,則沒有任何東西會達到l>lmax
。lista_eagle
函數中,您沒有返回任何內容,而是打印了結果。number * lmax
,它是一個數字而不是條紋本身,您可以通過將數組連接到自身 lmax 次來獲得它(如return [number] * lmax
)。正如其他人指出的那樣,聽起來您希望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'
在lista_egale()
的 print staement 中執行以下操作:
print((str(number)+" ")*lmax)
這將打印 2 2 2 而不是 222
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.