Jump to content

Fixing grammar in Python?

Go to solution Solved by Guest,
21 hours ago, Red Saum said:

EDIT: I think I figured it out, but is there a more efficient way to do this certain thing than:

 


def lumberjack(name, pronoun):
    if name[-1] == 's':
        print("{}' a lumberjack and {} OK!".format(name, pronoun))
    else:
        print("{}'s a lumberjack and {} OK!".format(name, pronoun))

lumberjack("Ashton", "he's")
lumberjack("Paris", "he's")
lumberjack("Sam", "they're")

 

One way you could make it a little more efficient is to use f-strings instead of .format(). An example:

def lumberjack(name, pronoun):
    if name[-1] == 's':
        print(f"{name}' a lumberjack and {pronoun} OK!")
    else:
        print(f"{name}'s a lumberjack and {pronoun} OK!")

lumberjack("Ashton", "he's")
lumberjack("Paris", "he's")
lumberjack("Sam", "they're")

Keep in mind this is a Python 3.6+ feature (A link for more comparisons) and there are some places where .format() is more appropriate.

def lumberjack(name, pronoun):
	print("{}'s a lumberjack and {} OK!".format(name, pronoun))

lumberjack("Ashton", "he's")
lumberjack("Paris", "he's")
lumberjack("Sam", "they're")

How do I fix this little program? Right now, the name Paris will print to "Paris's" rather than "Paris'", which is obviously grammatically incorrect. How do I fix this? I assume I'd want to use an if statement, but I don't really know how to implement that into this little bit of code. 

 

EDIT: I think I figured it out, but is there a more efficient way to do this certain thing than:

 

def lumberjack(name, pronoun):
    if name[-1] == 's':
        print("{}' a lumberjack and {} OK!".format(name, pronoun))
    else:
        print("{}'s a lumberjack and {} OK!".format(name, pronoun))

lumberjack("Ashton", "he's")
lumberjack("Paris", "he's")
lumberjack("Sam", "they're")

 

Link to comment
https://linustechtips.com/topic/875243-fixing-grammar-in-python/
Share on other sites

Link to post
Share on other sites

21 hours ago, Red Saum said:

EDIT: I think I figured it out, but is there a more efficient way to do this certain thing than:

 


def lumberjack(name, pronoun):
    if name[-1] == 's':
        print("{}' a lumberjack and {} OK!".format(name, pronoun))
    else:
        print("{}'s a lumberjack and {} OK!".format(name, pronoun))

lumberjack("Ashton", "he's")
lumberjack("Paris", "he's")
lumberjack("Sam", "they're")

 

One way you could make it a little more efficient is to use f-strings instead of .format(). An example:

def lumberjack(name, pronoun):
    if name[-1] == 's':
        print(f"{name}' a lumberjack and {pronoun} OK!")
    else:
        print(f"{name}'s a lumberjack and {pronoun} OK!")

lumberjack("Ashton", "he's")
lumberjack("Paris", "he's")
lumberjack("Sam", "they're")

Keep in mind this is a Python 3.6+ feature (A link for more comparisons) and there are some places where .format() is more appropriate.

Link to post
Share on other sites

On 12/19/2017 at 4:34 PM, Red Saum said:

How do I fix this little program? Right now, the name Paris will print to "Paris's" rather than "Paris'", which is obviously grammatically incorrect. How do I fix this? I assume I'd want to use an if statement, but I don't really know how to implement that into this little bit of code. 

 

Actually, when dealing with singular nouns both " XXXs's " and " XXXs' " are acceptable. When dealing with plural nouns that end in s only " XXXs' " is acceptable.

Luckily, since both forms are acceptable for singular nouns, just always using the " XXXs' " form for all nouns that end in "s" will be technically correct.

However, it's not always the best sounding way. Unfortunately, no good solution for that exists yet.

 

ENCRYPTION IS NOT A CRIME

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×