Reputation: 88
I'm trying to make a program deciding what determiner comes behind the input.
#For now, this only works with letters A-D.
MyInput = input("What word do you want to find out if the determiner is a/an?")
Finder = list(MyInput)
if ('a' in Finder):
print ("Start with an.")
else:
print ("Start with a.")
However, there is a little bug. When I input the word "bad", it says I need to have "an" go before the word. I want it to only say I need to have "an" be before the word when the first letter is A. What's the solution to this problem?
Upvotes: 0
Views: 126
Reputation: 624
It is because in
checks if the character can be found anywhere in the string. When you use "bad" it can. If you are checking the first character use Finder[0]
MyInput = input("What word do you want to find out if the determiner is a/an?")
Finder = list(MyInput)
if (Finder[0].lower() == 'a'):
print ("Start with an.")
else:
print ("Start with a.")
Upvotes: 1
Reputation: 138
Regarding the question, using str.startwith would be the most clear way to go:
# For now, this only works with letters A-D.
my_input = input("What word do you want to find out if the determiner is a/an?")
if my_input.startswith('a'):
print ("Start with an.")
else:
print ("Start with a.")
Pay attention to the fact you do not need to make a list from a string: strings are iterable, they support in
and indexing out of the box.
Also note the readability improvements:
See PEP8, the python style guide for more issues and details about codestyle.
Cheers!
Upvotes: 0