Python 程序从未进入第二个elif语句,我也不知道为什么
我目前正在做一项作业,分析一串字母,并打印出其中按字母顺序排列的最长部分。这是我为它编写的代码,但我收到的唯一输出是“az”,而不是预期的输出:“beggh”。在添加了一些print语句之后,我看到第二个elif语句只在循环的第一次迭代中执行,然后它基本上被卡在第一个elif中,永久性地增加end_字符串,而不再将其与max_end_字符串进行比较Python 程序从未进入第二个elif语句,我也不知道为什么,python,Python,我目前正在做一项作业,分析一串字母,并打印出其中按字母顺序排列的最长部分。这是我为它编写的代码,但我收到的唯一输出是“az”,而不是预期的输出:“beggh”。在添加了一些print语句之后,我看到第二个elif语句只在循环的第一次迭代中执行,然后它基本上被卡在第一个elif中,永久性地增加end_字符串,而不再将其与max_end_字符串进行比较 s = 'azcbbobobegghakl' end_string = '' max_end_string = '' for char_counte
s = 'azcbbobobegghakl'
end_string = ''
max_end_string = ''
for char_counter in range(len(s)):
if char_counter >= len(s)-1:
break
elif (s[char_counter] < s[char_counter + 1]) or (s[char_counter] > s[char_counter - 1]):
end_string = end_string + s[char_counter]
elif len(end_string) > len(max_end_string):
max_end_string = end_string
end_string = ''
print(max_end_string)
s='azcbbobegghakl'
结束字符串=“”
最大结束字符串=“”
对于范围(len)内的字符计数器:
如果字符计数器>=len(s)-1:
打破
elif(s[char\u counter]s[char\u counter-1]):
end_字符串=end_字符串+s[字符计数器]
elif len(结束字符串)>len(最大结束字符串):
最大结束字符串=结束字符串
结束字符串=“”
打印(最大结束字符串)
我已经修复了您的代码。祝你今天愉快
s = 'azcbbobobegghakl'
end_string = ''
max_end_string = ''
for char_counter in range(len(s)):
#skip check for char_counter == 0 since the first character is always alphabetical
#check if the current character is >= to previous alphabetically
if char_counter == 0 or s[char_counter] >= s[char_counter - 1]:
end_string += s[char_counter] #add current character to current string
else:
#check if the current string is the longest
if len(end_string) > len(max_end_string):
max_end_string = end_string
#add the current character to the next temp string
#Note: it is necessary to do this rather than simply clearing end_string
#(Consider the following case: "azabc")
end_string = s[char_counter]
#final check in case the longest string contains the last character
#(Consider the following case: "azabc")
if len(end_string) > len(max_end_string):
max_end_string = end_string
print(max_end_string)
这是另一个可能的解决方案,在我看来,这似乎更干净——你来做裁判 不过,我要提到的第一件事是,不必添加条件来检查当前字符的索引是否小于字符串的长度
for char_counter in range(len(s)):
if char_counter >= len(s)-1:
相反,对于范围(len(s)-1)中的字符计数器,只需从范围中减去1即可:
下面是我提出的解决方案(将变量名更改为更易于阅读的名称):
letters=“azcbbobegghakl”
最长=“”
温度=字母[0]
def字母顺序(字母,下一个字母):
#此语句可以在您的循环中使用
#为提高可读性,将其分解为自己的功能
如果字母len(最长):
#如果当前字母大于下一个字母,请检查
#我们的临时字符串比最长的字符串长
#如果是的话,那就是我们新的最长的。
#用下一个字母开始临时字符串
最长=温度
温度=字母[i+1]
其他:
#如果临时字符串短于最长字符串,则
#用下一个字母重新声明临时字符串
温度=字母[i+1]
打印(最长)
这正是elif
的工作原理。如果满足第一个elif
中的条件,则执行elif
下的代码,这就是If-elif-else语句的结尾。未检查第二个elif
中的条件。您应该使用新的if
而不是第二个elif
。但这只是这段代码的问题之一。elif不能在循环的下一次迭代中再次运行吗?还有什么其他问题?我对编程非常陌生,因此我不知道有多少比上面提到的更复杂的方法来解决这个问题。另一个问题是,你永远不会清除你的end\u字符串
,即使你不再连续增加字符。我建议重新考虑你的算法。你使用的是if语句,但是每种情况下的条件都不同,所以使用elif没有真正意义。我试着为每一个场合做单独的if,它会打印出“begg”。我用end_string=''来表示。这不会删除结束字符串内容吗?
letters = "azcbbobobegghakl"
longest = ""
temp = letters[0]
def alphabetical(letter, next_letter):
# This statement could be worked into your loop
# broken out into its own function for readability
if letter <= next_letter:
return True
else:
return False
for i in range(len(letters) - 1):
if alphabetical(letters[i], letters[i + 1]):
# if the current letter is less than the next, concatenate the next to our temporary string
temp += letters[i + 1]
elif len(temp) > len(longest):
# if the current letter is greater than the next letter check if
# our temporary string is longer than our longest string
# if so, that's our new longest.
# start the temp string over with the next letter
longest = temp
temp = letters[i + 1]
else:
# if the temp string is shorter than the longest then
# redeclare the temporary string with the next letter
temp = letters[i + 1]
print(longest)