1

I'm new to programming and am currently working through "Python Programming: An Introduction to Computer Science,2nd ed" by Zelle.

While working on one of the exercises in the book,I encountered some trouble understanding a solution provided by the author. The exercise is basically to make a program that gives a letter grade for a certain range of points.

The question is as follows: "A certain CS professor gives 100-points exams that are graded on the scale 90-100:A, 80-89:B, 70-79:C, 60-698:D, <60:F. Write a program that accepts an exam score as input and prints out the corresponding grade."

Here is my own source code for the exercise:

score = float(input("Enter your quiz score: "))

if score >= 90:
    print("You got an A.")
elif score >= 80:
    print("You got a B.")
elif score >= 70:
    print("You got a C.")
elif score >= 60:
    print("You got a D.")
else:
    print("You got a F.")

And it works perfectly well and from my searches,is a standard solution to such a problem.

Then,the author's solution is as follows:

score = eval(input("Enter the score (out of 100): "))
grades = 60*"F"+10*"D"+10*"C"+10*"B"+11*"A"
print("The grade is", grades[score])

Which I found to be so much neater as the entire if-elif-else chunk could be much more succinctly expressed with only 2 lines. However,I'm finding trouble trying to understand the 2nd line of his code: grades = 60*"F"+10*"D"+10*"C"+10*"B"+11*"A" How does this line work exactly and what does the * do in the case of a variable assignment such as this?

Pardon me if there's already a similar question to this that answers my query,but the closest I could find was about what * does in parameters. I would gladly appreciate a link to be directed there if that's that case.

Thanks for the help!

5
  • 1
    I'd be wary about this book. The use of a dangerous tool like eval is unnecessary, and while building a 101-character string isn't a problem, the solution doesn't scale well if applied to similar problems where the string would be longer. Commented Mar 24, 2016 at 2:17
  • Oh,that's a good point about scaling. Oh,yes,I understood the danger regarding the eval,which is why I used float(input()) instead of eval. Commented Mar 24, 2016 at 2:41
  • But thanks for the heads-up. I'll be wary! Commented Mar 24, 2016 at 2:41
  • A small remark: "You got an F" Commented Mar 24, 2016 at 3:14
  • Ah,yes. That too. Thanks. Commented Mar 25, 2016 at 10:37

3 Answers 3

4

This isn't really anything to do with variable assignment. In Python, you can multiply a string by a non-negative integer; the effect is to repeat the string the appropriate number of times. So, e.g., 5*"A"+2*"B" is "AAAAABB".

(So in the actual code you're looking at, you have 60 "F"s -- so grades[score] will be "F" when 0 <= score < 60 -- and then 10 "D"s -- so grades[score] will be "D" when 60 <= score < 70 -- and so on.)

Sign up to request clarification or add additional context in comments.

1 Comment

Oh,right! The answer was just so simple after all. Why did I thought it to be more complex than that? Haha,thanks!
2

It has nothing to do with assignment. 3*"F" is "FFF", as simple as that. "FFF"+"DDD" is "FFFDDD". grades is thus a string of 101 characters (sixty F's, ten D's... one for each score between 0 and 100), and you just pick the right one using grades[score].

Comments

0

The asterisk *will concatenate X versions of the preceding string, Where X is defined by the following number. The + will concatenate the preceding and following string

>>> "x"*2
'xx'
>>> "x"*2+"y"*2
'xxyy'
>>> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.