Firstly, I have <48 hours hands-on-keyboard learning Python over the last week.
I searched for similar questions, and they exist, but I'm looking for a solution that introtopython.org assumes I know based on where I am in the course. (Meaning no f-strings, no conditional expressions.)
Print out a series of sentences, "A judge can give a gymnast _ points." Don't worry if your first sentence is grammatically incorrect, but bonus points if you can figure it out with a slice.
My first attempt code:
# A gymnast can earn a score between 1 and 10 from each judge; nothing lower,
# nothing higher. All scores are integer values; there are no decimal scores
# from a single judge.
pscores = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
onepoint = pscores[:1]
for pscore in onepoint:
print("\nA judge can give a gymnast ",str(onepoint)," point.")
print("A judge can give a gymnast %d points." % (pscores[1]))
print("A judge can give a gymnast %d points." % (pscores[2]))
print("A judge can give a gymnast %d points." % (pscores[3]))
print("A judge can give a gymnast %d points." % (pscores[4]))
print("A judge can give a gymnast %d points." % (pscores[5]))
print("A judge can give a gymnast %d points." % (pscores[6]))
print("A judge can give a gymnast %d points." % (pscores[7]))
print("A judge can give a gymnast %d points." % (pscores[8]))
print("A judge can give a gymnast %d points.\n" % (pscores[9]))
Output:
A judge can give a gymnast [1] point.
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.
Super close. I can't figure out how to get the first printed line to display with the %d placeholder, without brackets. Also, I had a morepoints var in there when I was trying to figure out scores 2-10 in a for loop, but I couldn't get it to work (morepoints = pscores[1:])
I feel like I'm on the cusp of understanding how to do what I want without f-strings or conditional expressions (since I don't know how to use those yet).
Is it me, or is it the particular website I'm using giving me a shot to figure it out on my own based on what I've learned about lists and tuples?
EDIT: While I could get rid of the for loop I currently have and replace it with a line similar to the last 9, I'm trying to figure out how to condense all of this with slices and loops.
CodePudding user response:
A slice is always a list, even when it is only one element. So the variable onepoint
in your code is the list containing the very first element in pscores
.
in order to get the first element and not the list you can either grab the first index of pscores
or onepoint
.
score = pscores[0] # this
scrore = onepoint[0] # or this
print("\nA judge can give a gymnast ",str(score)," point.")
The reason why your for loop isn't working is because your iterator variable is pscore
but the variable you convert to a string is onepoint
. So this would also work:
for pscore in onepoint:
print("\nA judge can give a gymnast ",str(pscore)," point.")
If the goal is to use slices I think they probably mean slices of the string, the three lines below alone will get the results:
text = "A judge can give a gymnast points."
for pscore in range(1,11):
print(text[:-8], pscore, text[-7:])