简体   繁体   English

程序从不进入第二个 elif 语句,我不知道为什么

[英]Program never enters second elif statement, and I am not sure why

I am currently doing an assignment to analyze a string of letters , and print out the longest alphabetically ordered part of it.我目前正在做一项任务来分析一串字母,并打印出其中最长的字母顺序部分。 This is the code I wrote for it, but the only output I receive is 'az' instead of the expected output: 'beggh'.这是我为它编写的代码,但我收到的唯一输出是“az”,而不是预期的输出:“beggh”。 After adding some print statements I saw that the second elif statement only goes through on the loops first iteration, and then it is basically stuck in the first elif, permanently increasing end_string without ever comparing it to max_end_string again.在添加了一些打印语句之后,我看到第二个 elif 语句只在循环第一次迭代时通过,然后它基本上卡在第一个 elif 中,永久增加 end_string 而不再次将它与 max_end_string 进行比较。

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)

I have fixed your code.我已经修复了你的代码。 Have a good day.祝你有美好的一天。

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)

Here's another potential solution, which to my eyes seems cleaner -- you be the judge.这是另一种可能的解决方案,在我看来似乎更清晰——你来做判断。

First thing I want to mention though is that it's not necessary to add a conditional to check that the index of the current character is less than the length of the string.不过,我想提的第一件事是,没有必要添加条件来检查当前字符的索引是否小于字符串的长度。

for char_counter in range(len(s)):
    if char_counter >= len(s)-1:

instead just subtract 1 from the range for char_counter in range(len(s) - 1):而只是从for char_counter in range(len(s) - 1):减去1 for char_counter in range(len(s) - 1):

And here's the solution I came up with (changed the variable names to something easier for me to read):这是我想出的解决方案(将变量名称更改为更易于阅读的名称):

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM