简体   繁体   English

Python 元音吞噬者

[英]Python vowel eater

Good evening everyone..... i wrote a vowel eater program with the code below大家晚上好.....我用下面的代码写了一个元音吃程序

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)

It run fine but i want to use concatenation operation to ask python to combine selected letters into a longer string during subsequent loop turns, and assign it to the wordWithoutVowels variable..... I really appreciate any help or suggestions thanks in advance它运行良好,但我想使用串联操作要求 python 在后续循环中将选定的字母组合成一个更长的字符串,并将其分配给 wordWithoutVowels 变量.....我非常感谢任何帮助或建议提前感谢

is this what you need?这是你需要的吗?

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)

Another approach.另一种方法。 You can prepare a set of vowels you want to filter-out before-hand and then use str.join() to obtain you string:您可以预先准备一组要过滤掉的元音,然后使用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)

Prints (for example):打印(例如):

Please Enter a word: Hello World
Hll Wrld

or you can try this:或者你可以试试这个:

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)

Using str.replace() seems like a natural way to go for a problem like this对于这样的问题,使用 str.replace() 似乎是 go 的自然方法

Brute Force蛮力
Just go through all of the vowels.只需 go 贯穿所有元音。 And if they exist in input string, replace them如果它们存在于输入字符串中,请替换它们

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)

Please Enter a word: Hello World
HLL WRLD

Use a loop使用循环
This is a little more elegant.这更优雅一点。 And you won't have to convert the input to uppercase.而且您不必将输入转换为大写。

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)

Please Enter a word: Hello World
Hll Wrld

I'm sorry I didn't read the original post (OP) more carefully.对不起,我没有更仔细地阅读原始帖子(OP)。 Poster clearly asked for a way to do this by concatenation in a loop.海报明确要求通过循环连接来做到这一点。 So instead of excluding vowels, we want to include the good characters.因此,我们不想排除元音,而是要包括好字符。 Or instead of looking for membership, we can look for not in membership.或者,我们可以不寻找会员资格,而不是寻找会员资格。

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)

Please Enter a word: Hello World
Hll Wrld

Try this.尝试这个。 I think it's the easiest way:我认为这是最简单的方法:

word_without_vowels = ""
user_word = input("Enter a word")
user_word = user_word.upper()

for letter in user_word:
    # Complete the body of the loop.
    if letter in ("A","E","I","O","U"):
        continue
    word_without_vowels+=letter
print(word_without_vowels)
user_word =str(input("Enter a Word"))
# and assign it to the user_word variable.
user_word = user_word.upper()
vowels = ('A','E','E','I','O','U')
for letter in user_word:
    if letter in vowels:
        continue
    elif letter == vowels:
        letter = letter - vowels
    else:
        print(letter)

A one liner that does what you need is:一个可以满足您需要的衬垫是:

wordWithoutVowels = ''.join([ x for x in userWord if x not in 'aeiou'])

or the equivalent:或等价物:

wordWithoutVowels = ''.join(filter(lambda x: x not in 'aeiou', userWord))

The code is creating a list containing the letters in the string that are not vowels and then joining them into a new string.该代码正在创建一个列表,其中包含字符串中不是元音的字母,然后将它们连接到一个新字符串中。

You just need to figure out how to handle the lower/capital cases.您只需要弄清楚如何处理小写/大写的情况。 You could do x.lower() not in 'aeiou' , check if x is in 'aeiouAEIOU' , ... you have plenty of choices.您可以x.lower() not in 'aeiou' ,检查x is in 'aeiouAEIOU'中,...您有很多选择。

user_word = input('enter a word:')
user_word = user_word.upper()

for letter in user_word:
    if letter in ('A','E','I','O','U'):
        continue
    print(letter)
word_without_vowels = ""
vowels = 'A', 'E', 'I', 'O', 'U'

user_word = input('enter a word:')
user_word = user_word.upper()

for letter in user_word:
    if letter in vowels:
        continue
    word_without_vowels += letter
print(word_without_vowels)

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

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