简体   繁体   English

在 for 循环中使用.upper() 后字符串中的奇数大写字母

[英]Odd uppercase letters in string after using .upper() in a for loop

As the title says, the output contains some uppercase letters, but for the life of me I can't figure out why.正如标题所说,output 包含一些大写字母,但对于我来说,我无法弄清楚为什么。 <=== Read the entire post to understand the problem. <=== 阅读整篇文章以了解问题。 This is the code snippet which I'm assuming causes the 'error'.这是我假设导致“错误”的代码片段。

def translate(s):
    i = 0
    word = ""
    for letter in s:
        word += letter.upper() + (letter * i) + "-"
        i += 1

The function takes a string as input and returns a string with first letter being capital and the following letters being multiplied by 1 += 1 (+1 for each set of different letters), followed by "-". function 将字符串作为输入并返回一个字符串,其中第一个字母为大写字母,后面的字母乘以 1 += 1(每组不同的字母 +1),然后是“-”。 Example:例子:

Input and Output输入和 Output

Input: "ZpglnRxqenU"输入:“ZpglnRxqenU”

Expected Output: "Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu"预期 Output:“Z-Pp-Ggg-Lllll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu”

Actual Output: "Z-Pp-Ggg-Llll-Nnnnn-RRRRRR-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-UUUUUUUUUUU"实际Output:“Z-Pp-Ggg-Lllll-Nnnnn-RRRRRR-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-UUUUUUUUUUU”

The problem问题

As you can see, the R:s are all uppercase and so is the U:s.如您所见,R:s 都是大写的,U:s 也是如此。 My question is: Why are they uppercase?我的问题是:为什么它们是大写的? I know why the first letter is, and that's intended but there should never be more than one uppercase (the first letter) per section (a section being within the bounderies of "-" and "-").我知道为什么第一个字母是,这是有意的,但每个部分(一个部分在“-”和“-”的边界内)不应该超过一个大写字母(第一个字母)。

For further refrence: https://www.codewars.com/kata/5667e8f4e3f572a8f2000039/如需进一步参考: https://www.codewars.com/kata/5667e8f4e3f572a8f2000039/

OBS: I'm not doing this to get an answer to the codewars challenge. OBS:我这样做不是为了回答代码战挑战。

This is the entire code.这是整个代码。

def translate(s):
    i = 0
    word = ""
    for letter in s:
        word += letter.upper() + (letter * i) + "-"
        i += 1
    eq1 = list(word)
    eq1.reverse()
    eq1.remove("-")
    eq1.reverse()
    word = ""
    for y in eq1:
       word += y
    return word

Lines 7-12 are just my way of dealing with removing the last "-".第 7-12 行只是我处理删除最后一个“-”的方式。 The output would have an "-" at the end if it wasn't there.如果 output 不存在,则其末尾将有一个“-”。 I know it's a bad way of dealing with it but I just wanted to finish it as fast as possible.我知道这是一种不好的处理方式,但我只是想尽快完成它。

The R:s and U:s are uppercase because we multiply the first letter of the given character. R:s 和 U:s 是大写的,因为我们将给定字符的第一个字母相乘。 It totally slipped my mind that the character may be uppercase and therefor the multiplies of the letter will too be uppercase.我完全忘记了这个字符可能是大写的,因此字母的倍数也将是大写的。

The R's and U's are uppercase because in the initial string, they are uppercase. R 和 U 是大写的,因为在初始字符串中,它们是大写的。 They will stay uppercase unless you try to change them to lowercase.除非您尝试将它们更改为小写,否则它们将保持大写。
Here is your updated code:这是您更新的代码:

def translate(s):
    i = 0
    word = ""
    for letter in s:
        word += letter.upper() + (letter.lower() * i) + "-"
        i += 1

Notice the .lower() after letter in (letter * i) .注意(letter * i)中的letter后面的.lower() ) 。

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

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