Python: grammar correct in a loop
Asked Answered
S

5

5

"I was doing the follow exercise:

A gymnast can earn a score between 1 and 10 from each judge.

Print out a series of sentences, "A judge can give a gymnast _ points." Don't worry if your first sentence reads "A judge can give a gymnast 1 points." However, you get 1000 bonus internet points if you can use a for loop, and have correct grammar.

My code is this:

scores = (tuple(range(1,11)))
 
for score in scores:
   print (f'A judge can give a gymnast {score} points.')

And my output of course was:

  • A judge can give a gymnast 1 points.

  • A judge can give a gymnast 2 points.

  • A judge can give a gymnast 3 points.

  • A judge can give a gymnast 4 points.

  • A judge can give a gymnast 5 points.

  • A judge can give a gymnast 6 points.

  • A judge can give a gymnast 7 points.

  • A judge can give a gymnast 8 points.

  • A judge can give a gymnast 9 points.

  • A judge can give a gymnast 10 points.

The question is: How can I correct the grammar in my loop? I mean, when is "1 point" how can I make my loop to say "1 point" and not "1 points" and for the others scores maintain the "points"?

Steffi answered 17/2, 2022 at 3:1 Comment(0)
T
2

Heres another way of doing it. The answer above may(I don't know) be more efficient, in my opinion this is more readable for future developers.

scores = (tuple(range(1,11)))
 
for score in scores:
    if score == 1:
        print ('A judge can give a gymnast 1 point.')
    else:
        print (f'A judge can give a gymnast {score} points.')
Tamper answered 17/2, 2022 at 3:6 Comment(0)
Y
10

Conditionally add the s to the string:

print(f'A judge can give a gymnast {score} point{"s" if score > 1 else ""}.')
Yogini answered 17/2, 2022 at 3:4 Comment(0)
P
5

I conclude from the simplicity of this problem, that you are rather new to Python and Programming in general.

You should take a look at fundamental-concepts like flow-control and finding a solution shouldn't be hard at all.

https://docs.python.org/3/tutorial/controlflow.html

But still - to give you a concrete answer to the question, you could simply add an if-statement to your loop like this:

scores = (tuple(range(1,11)))
 
for score in scores: 
   output = f'A judge can give a gymnast {score} point'
   if score > 1: 
      output += "s"
   print(f"{output}.")
Polymyxin answered 17/2, 2022 at 3:9 Comment(0)
H
4

All you'll need is an if statement to check the score

scores = (tuple(range(1,11)))
 
for score in scores:
   if (score == 1):
       print (f'A judge can give a gymnast {score} point.')
   else:
       print (f'A judge can give a gymnast {score} points.')
Hydrus answered 17/2, 2022 at 3:5 Comment(0)
T
2

Heres another way of doing it. The answer above may(I don't know) be more efficient, in my opinion this is more readable for future developers.

scores = (tuple(range(1,11)))
 
for score in scores:
    if score == 1:
        print ('A judge can give a gymnast 1 point.')
    else:
        print (f'A judge can give a gymnast {score} points.')
Tamper answered 17/2, 2022 at 3:6 Comment(0)
G
2

A straight-forward way would be to use the python string format to conditionally render the s

print(f'A judge can give a gymnast {score} point{"s" if score > 1 else ""}.')
Gopher answered 17/2, 2022 at 3:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.