[英]Returning max sequence from a list
I am trying to write a program with a console UI where a user can input a sequence and if they press 2 or 3 they can find the longest equal integer sequence and the longest "mountain" sequence respectively我正在尝试编写一个带有控制台 UI 的程序,用户可以在其中输入一个序列,如果他们按 2 或 3,他们可以分别找到最长的等整数序列和最长的“山”序列
everything goes smoothly when I press 1, I can enter the list, but when I press 2 (I haven't implemented 3 yet) nothing happens当我按1时一切都很顺利,我可以进入列表,但是当我按2时(我还没有实现3)没有任何反应
Example Input : after pressing 1, 1 2 2 2 2 4, then press 2示例输入:按 1, 1 2 2 2 2 4 后,然后按 2
Expected output: 4预期输出:4
Actual output: nothing, the program doesn't work实际输出:无,程序不工作
(I hope the title is on topic) (我希望标题是关于主题的)
def lista_egale(lst1):
l = 0
prev_one = None
lmax = -1
for current in lst1:
if prev_one == current:
l += 1
elif l > lmax:
lmax = l
l = 0
prev_one = current
print(lmax)
def afiseaza_meniu():
print("Aplicatie cu liste")
print("1. Introduceti lista")
print("2. Verificati daca lista are o secventa maxima cu numere egale si afisati-o")
print("3. Verificati daca lista este de tip munte si afisati-o")
print("4. exit")
def ui_citire_lista(lst1):
input_string = input("Introduceti numerele ")
lst1 = input_string.split()
def ui_afisare_munte():
pass
def run():
global lst1
lst1 = []
afiseaza_meniu()
while True:
cmd = input(">>>")
if cmd == "4":
return
if cmd == "1":
ui_citire_lista(lst1)
elif cmd == "2":
lista_egale(lst1)
elif cmd == "3":
ui_afisare_munte()
else:
print("comanda invalida")
def main():
run()
#test_egale()
#test_munte()
if __name__ == "__main__":
main()
Your code doesn't work as your ui_citire_lista
only assigns lst1
locally.您的代码不起作用,因为您的
ui_citire_lista
仅在本地分配lst1
。 You should rather return the value and catch it in run
(where it is global)您应该返回值并在
run
捕获它(它是全局的)
change to ui_citire_lista
:更改为
ui_citire_lista
:
def ui_citire_lista():
input_string = input("Introduceti numerele ")
return input_string.split()
change to run
:更改为
run
:
if cmd == "1":
lst1 = ui_citire_lista()
NB.注意。 there are other issues, but I'll let you discover them by yourself ;) don't hesitate to post again if you have trouble
还有其他问题,但我会让你自己发现它们 ;) 如果你有问题,请不要犹豫再次发布
Here is a version of returning input, but you have to correct your lista_eagle function.这是返回输入的一个版本,但您必须更正 lista_eagle 函数。
def lista_egale(lst1):
l = 0
prev_one = None
lmax = -1
for current in lst1:
if prev_one == current:
l += 1
elif l > lmax:
lmax = l
l = 0
prev_one = current
# print(lmax)
return lmax
def afiseaza_meniu():
print("Aplicatie cu liste")
print("1. Introduceti lista")
print("2. Verificati daca lista are o secventa maxima cu numere egale si afisati-o")
print("3. Verificati daca lista este de tip munte si afisati-o")
print("4. exit")
def ui_citire_lista(lst1):
input_string = input("Introduceti numerele ")
lst1 = input_string.split()
def ui_afisare_munte():
pass
def run():
global lst1
lst1 = []
afiseaza_meniu()
result = None
while True:
cmd = input(">>>")
if cmd in ['1','2','3','4']:
lst1.append(cmd)
else:
print("comanda invalida")
if cmd == "4":
print('longest sequence is', result)
return result
elif cmd == "1":
ui_citire_lista(lst1)
elif cmd == "2":
result = lista_egale(lst1)
elif cmd == "3":
ui_afisare_munte()
def main():
run()
#test_egale()
#test_munte()
if __name__ == "__main__":
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.