我正在编写一个程序,用户输入数值到列表中,直到他们想要结束,并且程序将告诉用户他们输入的最长连续数字。例如,如果用户输入7、7、7、6、6、4、end,则会得到输出:你的最长连续是3。因为7连续输入了3次。
到目前为止,我的代码似乎不想结束当前运行,所以如果我输入7、7、7、6、6、6、6、5、4,它会告诉我最长的连续数字是7,就像它从输入的7开始继续计算一样。以下是我的代码:
到目前为止,我的代码似乎不想结束当前运行,所以如果我输入7、7、7、6、6、6、6、5、4,它会告诉我最长的连续数字是7,就像它从输入的7开始继续计算一样。以下是我的代码:
mylist = []
run = 1
currentrun = 1
number = input('enter a number: ')
mylist.append(number)
while number != 'end' :
number = input ('enter a number: ')
mylist.append(number)
for i in range (len(mylist)):
if mylist[i] == mylist[i-1] and mylist[i] == mylist[i+1] :
currentrun = currentrun + 1
else:
currentrun = 0
print (currentrun)
if currentrun > run:
run = currentrun
print (mylist)
print ('Your longest run was' ,run)
非常感谢您的帮助。
ValueError
。 - motoku