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
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)