0

I am trying to clean up my code for an assignment by running pylint over it with the google python style rc file. I just want to confirm that this is the correct style for the the first print line, as it look pretty weird, but the google style rcfile is showing that it is the correct style. I know that the length of each line mustn't exceed 80 characters

for position, length in STEPS:
    guess = prompt_guess(position, length)
    score = compute_score(guess, position, word)
    total = + total + score
    print("Your guess and score were: " + ('_' * position + str(guess) +
                                           ('_' * (len(word) - length -
                                                   position))) + " : " +
          str(score))
    print("")

I would've formatted it like this:

for position, length in STEPS:
    guess = prompt_guess(position, length)
    score = compute_score(guess, position, word)
    total = + total + score
    print("Your guess and score were: " + ('_' * position + str(guess) +
         ('_' * (len(word) - length -position))) + " : " + str(score))
    print("")

Any clarification would be appreciated, thanks

S. Mac
  • 3
  • 4
  • 1
    What about passing multiple argument to `print()`? You don't have to add strings to each other. – Łukasz Rogalski Aug 21 '17 at 15:17
  • I would like the guess and score on the same line though in the output Like this, Your guess and score were: at____ : 0 – S. Mac Aug 21 '17 at 15:19
  • `print("a", "b", "c")` -> `a b c` – Łukasz Rogalski Aug 21 '17 at 15:20
  • Doing string conversions and `+` concatenation inside a `print` call is an anti-pattern. Let `print` do the conversion & concatenation for you: it's cleaner & more efficient. And if you need more control over the spacing of the output use `.format` (or f-strings in Python 3.6). BTW, the `format` method (or `format` function) can also do padding. – PM 2Ring Aug 21 '17 at 15:24

3 Answers3

1

You shouldn't build your string inside print. When it comes to a very long message, take several steps to build it.

s = "Your guess and score were: "
s += '_' * position
s += str(guess)
s += '_' * (len(word) - length - position)
s += " : "
s += str(score))

You can make it a bit cleaner by using the str.format method. The parameters will replace the curly braces, according to the names given:

pad1 = '_' * position
pad2 = '_' * (len(word) - length - position)
s = "Your guess and score were: {pad1}{guess}{pad2} : {score}"
s = s.format(pad1=pad1, pad2=pad2, guess=guess, score=score)

This allows you to indent the parameters as a listing, in case their names are long:

s = s.format(pad1=pad1,
             pad2=pad2,
             guess=guess,
             score=score)

If the definition of each parameter is short enough, you can send it to the format method:

s = "Your guess and score were: {pad1}{guess}{pad2} : {score}"
s = s.format(pad1='_' * position,
             pad2='_' * (len(word) - length - position),
             guess=guess,
             score=score)

If your string has a lot of values to be interpolated, you can get rid of the variable names, but then, the curly braces will be replaced by the parameters in the same order:

s = "Your guess and score were: {}{}{} : {}"
s = s.format(pad1, guess, pad2, score)
Right leg
  • 16,080
  • 7
  • 48
  • 81
  • This was exceptionally helpful, thank you very much. I've edited the code above. – S. Mac Aug 21 '17 at 15:43
  • @S.Mac You're welcome. Respecting that 80 characters limit is a pain in the neck, isn't it :) – Right leg Aug 21 '17 at 15:45
  • @S.Mac That's a good initiative to be willing to improve one's coding style, as it's most of the time what makes the difference between good and bad code. Anyway, you really should have a look at the [PEP8](https://www.python.org/dev/peps/pep-0008/), it will guide you on this track. – Right leg Aug 21 '17 at 15:48
0

See PEP-8 on indentation:

# YES: Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)

# NO: Arguments on first line forbidden when not using vertical alignment.
foo = long_function_name(var_one, var_two,
    var_three, var_four)

(Consistent with Google Python Style Guide on indentation.)

Also, should a line break before or after a binary operator?:

# NO: operators sit far away from their operands
income = (gross_wages +
          taxable_interest +
          (dividends - qualified_dividends) -
          ira_deduction -
          student_loan_interest)

# YES: easy to match operators with operands
income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)
randomir
  • 17,989
  • 1
  • 40
  • 55
0

Correct, indentation depends on previous line's parentheses. But readability is more than just passing pylint, consider:

print("Your guess and score were: {PAD1}{GUESS}{PAD2} : {SCORE}"
      "".format(PAD1='_' * position,
                GUESS=guess,
                PAD2='_' * (len(word) - length - position),
                SCORE=score))

(Use of string concatenation makes for easier formatting of longer strings.)

pbuck
  • 4,291
  • 2
  • 24
  • 36