Posts: 2
Threads: 2
Joined: Feb 2021
Feb-25-2021, 09:54 PM
(This post was last modified: Feb-26-2021, 12:46 PM by buran.)
Hi everyone,
I am running into a task that needs your help.
The task is:
Ask for user input, and write a for loop that will output all the vowels within it. For example:
>>> "Hello" ➔ "eo"
My code is here:
word = input("Write your word here: ")
print(word)
for letter in word:
if letter == 'aeiou':
print(letter) I really appreciate your help!
Thank you.
buran write Feb-26-2021, 12:46 PM:Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Posts: 6
Threads: 2
Joined: Feb 2021
i found the solution
my code:
word = input("Write your word here: ")
print(word)
for letter in word:
if letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o' or letter == 'u':
print(letter) as you can see here i added alot of ors, which basically in human will be like this:
hey python, if the letter variable equals to string a, or the letter variable equals to string b, or the letter variable equals to string i, or the letter variable equals to string o, or the letter variable equals to string u, then print the letter variable
Posts: 101
Threads: 0
Joined: Jan 2021
Neither of your solutions match the example given in the task and what about upper case letters:
Output: "All the happy people" -> "Aeaeoe"
It is a quite simple beginners problem that you ought to be able to solve after studying pythons print command e.g. read about print() on RealPython
Posts: 6,252
Threads: 16
Joined: Feb 2020
I don't like printing a letter at a time and I want more control over formatting.
word = input('Enter a word ')
print(f"{word} -> {''.join([a for a in word if a in 'aeiouAEIOU'])}")
Posts: 101
Threads: 0
Joined: Jan 2021
Feb-26-2021, 08:20 AM
(This post was last modified: Feb-26-2021, 08:20 AM by Serafim.
Edit Reason: Better formatting
)
@ deanhystad I also like to collect everything prior to printing (or whatever) and I also like good control over formatting but in a beginners context where one has come to the point where for loops have just been introduced I think that this is a slightly more plausible approach:
txt = input('Write something: ') # No mention of a single word in the task
print(f'"{txt}" -> "', end = '')
for c in txt:
#<continue printing if c is a wowel>
print('"') Where substituting code for the last comment is a nobrainer if they study both our examples, and it meets exactly the example in the task text.
Posts: 1,936
Threads: 8
Joined: Jun 2018
(Feb-25-2021, 10:13 PM)NullAdmin Wrote: word = input("Write your word here: ")
print(word)
for letter in word:
if letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o' or letter == 'u':
print(letter) as you can see here i added alot of ors
I would suggest to replace row #5 with following:
if letter.lower() in 'aeiou': This will get rid of repeating or's and also catches uppercase vowels.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 2,024
Threads: 9
Joined: May 2017
A combination of "".join() and a generator expression with conditional.
The conditional does containment checking for each char. Before the comparison is made, the char is made lower case.
VOWELS = "aeiou"
text = input("Enter your text: ")
only_vowels = "".join(char for char in text if char.lower() in VOWELS)
no_vowels = "".join(char for char in text if char.lower() not in VOWELS)
Posts: 6,252
Threads: 16
Joined: Feb 2020
Feb-26-2021, 03:08 PM
(This post was last modified: Feb-26-2021, 03:09 PM by deanhystad.)
I don't use join very often (hardly at all) and was unaware that you can use a condition in the iterator. Good to know.
And that is why I used join and the list comprehension in my reply. To only answer the question the answer provides the least amount of information and the questioner is left with the most amount of ignorance. Yes, you can print out a letter at a time using additional arguments in the print statement, but nobody would write the solution that way. Yes you can specify separators and ends and all kinds of things in the print statement, but almost everyone uses some sort of formatter instead.
Now PP904 knows that you can change the end character for a print statement to be a space or a blank instead of a newline, that there is a way to join multiple characters into a string, and that you can specify output format when printing. Just like I now know that I can control what is joined when using the join() string method.
Posts: 101
Threads: 0
Joined: Jan 2021
(Feb-26-2021, 03:08 PM)deanhystad Wrote: I don't use join very often (hardly at all) and was unaware that you can use a condition in the iterator. Good to know.
And that is why I used join and the list comprehension in my reply. To only answer the question the answer provides the least amount of information and the questioner is left with the most amount of ignorance. Yes, you can print out a letter at a time using additional arguments in the print statement, but nobody would write the solution that way. Yes you can specify separators and ends and all kinds of things in the print statement, but almost everyone uses some sort of formatter instead.
Now PP904 knows that you can change the end character for a print statement to be a space or a blank instead of a newline, that there is a way to join multiple characters into a string, and that you can specify output format when printing. Just like I now know that I can control what is joined when using the join() string method. Sorry to have hurt your feelings. I simply want us, who supposedly are experienced programmers (and maybe teachers), to try to understand the students situation and programming skill level and try to adapt to it. Sometimes solutions given on this site are far above the apparent level of the person posting the question, which I think is more bewildering than helpful. I will however be more careful in my comments from now on.
|