"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"?
CodePudding user response:
Conditionally add the s
to the string:
print(f'A judge can give a gymnast {score} point{"s" if score > 1 else ""}.')
CodePudding user response:
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.')
CodePudding user response:
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}.")
CodePudding user response:
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.')
CodePudding user response:
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 ""}.')