2017-08-21 62 views
0

我想清理我的代碼,通過運行pylint與谷歌python風格rc文件的代碼。我只是想確認這是第一條打印線的正確風格,因爲它看起來很奇怪,但谷歌風格的rcfile顯示它是正確的風格。我知道,每行的長度不得超過80個字符如何在python中使用Google python style和pylint設計長線?

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

我會一直這樣格式化它:

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

任何澄清,將不勝感激,謝謝

+1

怎麼樣傳遞多個參數'打印()'?您不必爲彼此添加字符串。 –

+0

我想在猜測和分數在同一行雖然在輸出 像這樣, 你的猜測和得分是:at____:0 –

+0

'print(「a」,「b」,「c」)' >'abc' –

回答

0

你不應該建立內部print您的字符串。 當涉及到很長的消息時,需要採取幾個步驟來構建它。

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

您可以通過使用str.format方法使其更清潔一點。 參數將取代大括號,按照給定的名字:

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) 

這可以縮進的參數列表,在他們的名字很長:

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

如果定義如果字符串有很多值

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

要插補:每個參數足夠短,你可以將它發送到format方法,你可以擺脫變量名,但後來,花括號將被以相同的順序將參數改爲:

s = "Your guess and score were: {}{}{} : {}" 
s = s.format(pad1, guess, pad2, score) 
+0

這非常有幫助,非常感謝。我編輯了上面的代碼。 –

+0

@ S.Mac不客氣。尊重80個字符的限制是一個痛苦的脖子,是不是:) –

+0

它真的是哈哈!我每天都在學習更多東西,再次感謝 –

0

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) 

(符合Google Python Style Guide on indentation

此外,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) 
0

正確,壓痕依賴於前行的括號內。但可讀性超過剛好路過pylint的,考慮:(字符串連接的使用使得對長字符串的簡單格式化)

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