#๐Ÿ”’ An f string that is a sentence with an embedded item from a list.

55 messages ยท Page 1 of 1 (latest)

topaz reef
#

how do I change the article "a" to "an" when needed as it precedes input that sometimes starts with a vowel and therefore changes the grammar of the sentence.

ex: motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
motorcycles.append('harley')
motorcycles.append('kawasaki')
motorcycles.append('aprilia')
motorcycles.append('indian')
motorcycles.append('bmw')
motorcycles.insert(0, 'ducati')
del motorcycles [1]
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

last_owned = popped_motorcycle
print(f"the last motorcycle I owned was a {motorcycles.pop()}")

if the input is "aprilia" or "Indian" the "a" before {motorcycles.pop()} needs to be changed to "an"

I also would love to know a more precise way to eplain this so that Im no only learning to code but also learning to communicate as a developer.

thanks for reading!

spark loomBOT
#

@topaz reef

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

undone trout
#

this is probably a bit more complex than you think

#

there are cases where it will become "an" without the word beginning with a vowel

topaz reef
#

Well I think I have done it before but I forget not

#

oh right that's true also.. I thought of an if clause???

undone trout
#

you mean like

f'{"an" if word[0].lower() in ["a","e","i","o","u"] else "a"}'
undone gull
#

there's a lot of edge cases to deal with here

#

regex could get you close

topaz reef
#

Yay!

undone trout
#

as fashoomp said, there are a lot of cases where this won't be a correct solution

undone gull
#

!e

import re

message = 'a snake, a elephant, a apple, a computer, a umbrella'

print(re.sub('a ([aeiou]+)', r'an \1', message))
spark loomBOT
topaz reef
#

yes siqius something like that.

undone trout
topaz reef
#

do both solutions work?

undone trout
#

regex solution is probably better

topaz reef
#

ok. thanks. what is regex?

undone trout
#

string manipulation

undone gull
#

my example looks for the letter "a", followed by a space, followed by a word starting with a vowel

#

actually sorry it's a bit wrong right now

#

print(re.sub('a ([aeiou][a-z]+)', r'an \1', message))

#

a ([aeiou][a-z]+)

#

this is the pattern

#

it's looking for letter a, followed by a space, followed by a vowel, followed by any number of letters

topaz reef
#

ok, thanks so much!!! how do you attach that inside the code I already wrote? I just tried it and it's not correct.

undone gull
#

store the sentence you want to print in a variable first

#

then re.sub that string

#

the top is the pattern, and then you can put in text to match it against

#

if it gets highlighted, then it's a match

undone trout
undone gull
#

yes, instead of printing it, store it in a variable and re.sub it afterwards

slender nova
#

an accurate a/an regex would be interesting to see, you'd have to take into account sound rules on words starting with u, irregular words like honour, acronyms... y as a vowel? the simple vowel checker is good enough though

topaz reef
#

import re
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
motorcycles.append('harley')
motorcycles.append('kawasaki')
motorcycles.append('aprilia')
motorcycles.append('indian')
motorcycles.append('bmw')
motorcycles.insert(0, 'ducati')
del motorcycles [1]
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

last_owned = popped_motorcycle
articles = 'a snake', 'an elephant', 'an apple', 'a computer', 'an umbrella'
print(f"the last motorcycle I owned was a {motorcycles.pop()}")
print(re.sub('a ([aeiou]+)', r'an \1', articles))

#

this is what i did so far...

#

py

topaz reef
#

Thanks so much @undone trout and @undone gull '''py import re
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
motorcycles.append('harley')
motorcycles.append('kawasaki')
motorcycles.append('aprilia')
motorcycles.append('indian')
motorcycles.append('bmw')
motorcycles.insert(0, 'ducati')
del motorcycles [1]
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

last_owned = popped_motorcycle
message = (f"the last motorcycle I owned was a {motorcycles.pop()}")
print(re.sub('a ([aeiou]+)', r'an \1', message))

#

I got it now!

undone gull
topaz reef
#

///py

#

sorry trying to format my code

topaz reef
topaz reef
#

`import re
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
motorcycles.append('harley')
motorcycles.append('kawasaki')
motorcycles.append('aprilia')
motorcycles.append('indian')
motorcycles.append('bmw')
motorcycles.insert(0, 'ducati')
del motorcycles [1]
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

last_owned = popped_motorcycle
message = (f"the last motorcycle I owned was a {motorcycles.pop()}")
print(re.sub('a ([aeiou]+)', r'an \1', message))`

#
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
motorcycles.append('harley')
motorcycles.append('kawasaki')
motorcycles.append('aprilia')
motorcycles.append('indian')
motorcycles.append('bmw')
motorcycles.insert(0, 'ducati')
del motorcycles [1]
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

last_owned = popped_motorcycle
message = (f"the last motorcycle I owned was a {motorcycles.pop()}")
print(re.sub('a ([aeiou]+)', r'an \1', message))```
spark loomBOT
#

Hey @topaz reef!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
topaz reef
#
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
motorcycles.append('harley')
motorcycles.append('kawasaki')
motorcycles.append('aprilia')
motorcycles.append('indian')
motorcycles.append('bmw')
motorcycles.insert(0, 'ducati')
del motorcycles [1]
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

last_owned = popped_motorcycle
message = (f"the last motorcycle I owned was a {motorcycles.pop()}")
print(re.sub('a ([aeiou]+)', r'an \1', message))```
#
import re
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
motorcycles.append('harley')
motorcycles.append('kawasaki')
motorcycles.append('aprilia')
motorcycles.append('indian')
motorcycles.append('bmw')
motorcycles.insert(0, 'ducati')
del motorcycles [1]
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

last_owned = popped_motorcycle
message = (f"the last motorcycle I owned was a {motorcycles.pop()}")
print(re.sub('a ([aeiou]+)', r'an \1', message))```
#
import re
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
motorcycles.append('harley')
motorcycles.append('kawasaki')
motorcycles.append('aprilia')
motorcycles.append('indian')
motorcycles.append('bmw')
motorcycles.insert(0, 'ducati')
del motorcycles [1]
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

last_owned = popped_motorcycle
message = (f"the last motorcycle I owned was a {motorcycles.pop()}")
print(re.sub('a ([aeiou]+)', r'an \1', message))```
spark loomBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.