[英]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()
、 upper
、 islower()
、 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.