-3

I hope to ask for help regarding this code.
Task is to omit the vowels and print the non-vowels only.

Task 1

I could only omit 1 vowel at time.

# Prompt the user to enter a word
# and assigne it to the user_word variable.

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

for letter in user_word:
    if letter == 'A':
        continue
    print(letter)
Please enter a word:alaska
L
S
K

How do I correct the code to include all vowels? Thank you.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 3
    You meant to write: `if letter in ('A','E','I','O','U'):` – quamrana Sep 01 '23 at 15:27
  • To help you underway, you can use the `in` operator to check if an element is contained in a list. – B Remmelzwaal Sep 01 '23 at 15:28
  • I have tried that, but it is giving me an error. – moon light Sep 01 '23 at 15:29
  • @quamrana You could also use `'AEIOU'`, and you're missing `letter.upper()`, see the example. – B Remmelzwaal Sep 01 '23 at 15:31
  • You can do something like `vowels = ['A', 'B', ...]` and then `if letter in vowels:` – Marco Balo Sep 01 '23 at 15:31
  • 1
    @BRemmelzwaal: Yes, `if letter in 'AEIOU':` is much better. (Also, not missing any `.upper()`. I've actually tried the code.) – quamrana Sep 01 '23 at 15:32
  • Great! This one works! 'AEIOU' :)) Thanks! (Yes, I already have the 'upper' code line) – moon light Sep 01 '23 at 15:33
  • 4
    Please don't post images of code. You should replace the images with the code as text surrounded by ``` (three backticks). – Craig Sep 01 '23 at 15:34
  • @quamrana Completely missed the upper applied on the word before the loop, you're right. – B Remmelzwaal Sep 01 '23 at 15:34
  • @craig oh...my bad... havent posted here for a while.. thanks for the reminder! – moon light Sep 01 '23 at 15:36
  • We like answers that have working code. But its really bad form to expect people to type in code from your image instead of copying. You should post the text, not images. – tdelaney Sep 01 '23 at 15:45
  • Of course, you could make it one line shorter by doing `if letter not in "AEIOU": print(letter)`, so you don't need the `continue`. Although this is probably slightly less efficient as every vowel has to be checked and it can't break out early. – Matt Pitkin Sep 01 '23 at 15:46
  • "How do I correct the code to include all vowels?" - well, what part of the code do you think checks whether the letter is an `A`, currently? How would you write code that checks whether a letter is a vowel, instead? What happens if you try putting the new code in place of the old code? (And **why do you imagine that any of this has anything to do with the `continue` statement?**) – Karl Knechtel Sep 01 '23 at 15:56
  • Before posting a question on Stack Overflow, please try to identify **the question that you are trying to get answered, not** simply the problem you are trying to solve. Then try to look for existing versions of that question. – Karl Knechtel Sep 01 '23 at 15:58

1 Answers1

1

Trying to keep it as similar as possible to the existing code, you could add 'or' in the if statement and compare to the rest of the vowels, i.e.

if letter == 'A' or letter == 'E' or letter == 'I' or letter == 'O' or letter == 'U':    
    continue
David
  • 11
  • 2
  • 2
    Would definitely use the `in` operator. This doesn't scale well and is harder to maintain because of all the repetition. – B Remmelzwaal Sep 01 '23 at 15:37
  • @BRemmelzwaal in that case should 'AEIOU' better be outside the for loop? Would python create a new string literal in memory every iteration or would it be smart enough to realise it's the same? – David Sep 01 '23 at 15:42
  • @moonlight my pleasure :) – David Sep 01 '23 at 15:44
  • @David I'm fairly certain Python would know. But don't quote me on it. – B Remmelzwaal Sep 01 '23 at 15:44
  • 1
    @BRemmelzwaal thanks, maybe someone can confirm that, will do some research myself and come back with an answer if I figure it out – David Sep 01 '23 at 15:50
  • From [this q&a](https://stackoverflow.com/questions/35402327/does-python-create-an-object-for-string-constants-in-equality-comparisons/35403444#35403444) we see that "String literals, like all Python constants, are created during compile time, when the source code is translated to byte code. And because all Python strings are immutable the interpreter can re-use the same string object if it encounters the same string literal in multiple places." Thus, no need to declare outside for loop. – DarrylG Sep 01 '23 at 16:30
  • @DarrylG thanks! I also found [this thread](https://stackoverflow.com/questions/15541404/python-string-interning) about string interning. – David Sep 01 '23 at 16:37
  • Also, `id('AEIOU')` is the same value inside a loop – David Sep 01 '23 at 16:45