简体   繁体   English

如何在列表中交替使用字母/单词的大小写?

[英]How do I alternate the cases of letters/words in a list?

I need to convert the "letter_guessed" input to lower case, if it's uppercase and if an uppercase letter already exists inside the list as a lower case it will return false but I can't get it to work.我需要将“letter_guessed”输入转换为小写,如果它是大写,并且如果列表中已经存在一个大写字母作为小写,它将返回 false 但我无法让它工作。

I have tried using isupper() , upper , islower() , lower() in many ways.我曾尝试以多种方式使用isupper()upperislower()lower() I am pretty sure that I am doing something wrong with "if" but can't get it right.我很确定我在“if”上做错了什么,但无法正确解决。

def check_valid_input(letter_guessed, old_letters_guessed):
    while True:
        """ will work only if you enter one letter and do not contain special letters other then the abc 
        and if its all ready been entered it will show false """

        if len(letter_guessed) == 1  and letter_guessed not in old_letters_guessed :
            """if the letter is one letter  and not already inside old_letter_guessed only then  continue """

            old_letters_guessed.append(letter_guessed)
            print("True")
            letter_guessed = input(" : ")

        else:
            """ if its wrong input will print False Try again and if the input is correct it will go back to " if " """

            #old_letters_guessed.append(letter_guessed)
            print(False, 'Try again')
            old_letters_guessed.sort()
            print('->'.join(old_letters_guessed))
            letter_guessed = input(" : ")
        #if letter_guessed is letter_guessed.isupper()

new = input()
old = []
check_valid_input(new,old)

Everything sting has a method called swapcase which swaps between upper and lower case.所有东西都有一个叫做 swapcase 的方法,它可以在大写和小写之间进行交换。 So:所以:

"TEST".swapcase()

Would become会成为

"test"

I'd suggest using that instead of your if statements.我建议使用它而不是你的 if 语句。

use a for loop to iterate over all elements in the list with the .swapcase() function common to all strings使用 for 循环遍历列表中的所有元素,并使用所有字符串通用的.swapcase()函数

list = ['some', 'words', 'in a list']
for word in list:
    print(word.swapcase())

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

相关问题 如何从具有特定字母的列表中查找单词? - How do I find words from a list with specific letters? (Python)如何将几个字母与整个单词列表进行比较以查找哪些单词按顺序包含搜索的字母? - (Python) How do I compare a few letters to a whole list of words to find, what words contain the searched-for letters in order? 如何从具有字母的行和列中获取单词? - How do i get words from rows and columns having letters? 我如何让这个代码计算单词而不是字母? - How do i get this code to count words not letters? Pandas 不能识别单词,只能识别字母。 当我切片时我该怎么做,它给我的是单词而不是字母? - Pandas is not recognizing words, but only letters. How can I do so when I slice, it gives me the words not the letters? Turtle Graphics-单词中字母的替代颜色 - Turtle Graphics - Alternate Colors of Letters in Words 为什么我得到字母而不是单词的频率? - Why do I get a frequency of letters and not words? 如何修复此程序,以便可以计算字母数和单词数? - How do I fix this program so I can count the number of letters and how do I count words? 如何将字符串拆分为单词列表? - How do I split a string into a list of words? 我如何将一堆单词变成一个列表? - how do i make a bunch of words into a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM