Python 巨蟒元音食者
大家晚上好。。。。。我用下面的代码写了一个吃元音的程序Python 巨蟒元音食者,python,Python,大家晚上好。。。。。我用下面的代码写了一个吃元音的程序 wordWithoutVowels = "" userWord = input("Please Enter a word: ") userWord = userWord.upper() for letter in userWord: if letter == 'A': continue elif letter == 'E': continue elif letter == 'I':
wordWithoutVowels = ""
userWord = input("Please Enter a word: ")
userWord = userWord.upper()
for letter in userWord:
if letter == 'A':
continue
elif letter == 'E':
continue
elif letter == 'I':
continue
elif letter == 'O':
continue
elif letter == 'U':
continue
else:
print(letter)
它运行得很好,但我想使用连接操作让python在后续循环中将选定的字母组合成一个较长的字符串,并将其分配给WordWithOut元音变量。。。。。
我非常感谢您的帮助或建议,提前谢谢您这是您需要的吗
wordWithoutVowels = ""
userWord = input("Please Enter a word: ")
userWord = userWord.upper()
for letter in userWord:
if letter == 'A':
word = letter
continue
elif letter == 'E':
continue
elif letter == 'I':
continue
elif letter == 'O':
continue
elif letter == 'U':
continue
else:
wordWithoutVowels+=letter
print(wordWithoutVowels)
这是你需要的吗
wordWithoutVowels = ""
userWord = input("Please Enter a word: ")
userWord = userWord.upper()
for letter in userWord:
if letter == 'A':
word = letter
continue
elif letter == 'E':
continue
elif letter == 'I':
continue
elif letter == 'O':
continue
elif letter == 'U':
continue
else:
wordWithoutVowels+=letter
print(wordWithoutVowels)
另一种方法。您可以事先准备一组要过滤掉的元音,然后使用
str.join()
获取字符串:
userWord = input("Please Enter a word: ")
vowels = set('aeiou')
wordWithoutVowels = ''.join(character for character in userWord if not character.lower() in vowels)
print(wordWithoutVowels)
打印(例如):
另一种方法。您可以事先准备一组要过滤掉的元音,然后使用
str.join()
获取字符串:
userWord = input("Please Enter a word: ")
vowels = set('aeiou')
wordWithoutVowels = ''.join(character for character in userWord if not character.lower() in vowels)
print(wordWithoutVowels)
打印(例如):
对于这样的问题,使用str.replace()似乎是一种自然的解决方法
暴力
把所有的元音都念一遍。如果它们存在于输入字符串中,则替换它们
wordWithoutVowels = ""
userWord = input("Please Enter a word: ")
userWord = userWord.upper()
# make a copy of userWord
output = userWord
# replace vowels
output = output.replace('A', '') # replace A with empty string
output = output.replace('E', '') # replace E with empty string
output = output.replace('I', '') # replace I with empty string
output = output.replace('O', '') # replace O with empty string
output = output.replace('U', '') # replace U with empty string
print(output)
使用循环
这个稍微优雅一点。您不必将输入转换为大写
wordWithoutVowels = ""
userWord = input("Please Enter a word: ")
# make a copy of userWord
output = userWord
# replace vowels
vowels = 'aAeEiIoOuU'
for letter in vowels:
output = output.replace(letter, '') # replace letter with empty string
print(output)
对于这样的问题,使用str.replace()似乎是一种自然的解决方法
暴力
把所有的元音都念一遍。如果它们存在于输入字符串中,则替换它们
wordWithoutVowels = ""
userWord = input("Please Enter a word: ")
userWord = userWord.upper()
# make a copy of userWord
output = userWord
# replace vowels
output = output.replace('A', '') # replace A with empty string
output = output.replace('E', '') # replace E with empty string
output = output.replace('I', '') # replace I with empty string
output = output.replace('O', '') # replace O with empty string
output = output.replace('U', '') # replace U with empty string
print(output)
使用循环
这个稍微优雅一点。您不必将输入转换为大写
wordWithoutVowels = ""
userWord = input("Please Enter a word: ")
# make a copy of userWord
output = userWord
# replace vowels
vowels = 'aAeEiIoOuU'
for letter in vowels:
output = output.replace(letter, '') # replace letter with empty string
print(output)
很抱歉,我没有更仔细地阅读原始帖子(OP)。海报明确要求通过在循环中串联来实现这一点。因此,我们不排除元音,而是希望包含好的字符。或者,我们可以寻找非会员,而不是寻找会员
wordWithoutVowels = ""
userWord = input("Please Enter a word: ")
vowels = 'aAeEiIoOuU'
wordWithoutVowels = '' # initialize to empty string
for letter in userWord:
if letter not in vowels:
wordWithoutVowels += letter # equivalent to wordWithoutVowels = wordWithoutVowels + letter
print(wordWithoutVowels)
很抱歉,我没有更仔细地阅读原始帖子(OP)。海报明确要求通过在循环中串联来实现这一点。因此,我们不排除元音,而是希望包含好的字符。或者,我们可以寻找非会员,而不是寻找会员
wordWithoutVowels = ""
userWord = input("Please Enter a word: ")
vowels = 'aAeEiIoOuU'
wordWithoutVowels = '' # initialize to empty string
for letter in userWord:
if letter not in vowels:
wordWithoutVowels += letter # equivalent to wordWithoutVowels = wordWithoutVowels + letter
print(wordWithoutVowels)
或者您可以尝试以下方法:
wordWithoutVowels = ""
user = input("Enter a word: ")
userWord = user.upper()
for letter in userWord:
if letter == "A":
continue
elif letter == "E":
continue
elif letter == "O":
continue
elif letter == "I":
continue
elif letter == "U":
continue
else:
wordWithoutVowels += letter
print(wordWithoutVowels)
或者您可以尝试以下方法:
wordWithoutVowels = ""
user = input("Enter a word: ")
userWord = user.upper()
for letter in userWord:
if letter == "A":
continue
elif letter == "E":
continue
elif letter == "O":
continue
elif letter == "I":
continue
elif letter == "U":
continue
else:
wordWithoutVowels += letter
print(wordWithoutVowels)
这回答了你的问题吗
eater=lambda s:''.join(filter(lambda c:c.upper()不在{'A','E','I','O','U'},s中))
谢谢,但我只需要在循环@Janez KuharDoes中进行一个简单的连接这回答了你的问题吗eater=lambda s:''.join(过滤器(lambda c:c.upper()不在{'A','E','I','O','U'},s中))
谢谢,但我只需要在循环@Janez KuharWhy不简单使用replace()
?为什么不简单使用replace()
?