2017-02-12 96 views
-1

我創建了一個計算程序,計算出一個等級,並吐出一條消息,如「你有一個(如果百分比超過90),b如果超過80等百分比..我不是。?!使用運營商正確地有人請告訴我正確的格式感謝布爾操作符<, >,<=等

def totals(score_w, score_x, score_y, score_z): 
print ("Overall percentage = ", round(score_w + score_x + score_y + score_z), 1) 
if round((score_w + score_x + score_y + score_z, 1) > 90.0): 
    print("Your grade will be at least: A") 
elif round((score_w + score_x + score_y + score_z, 1) (< 90) and (>= 80)): 
    print("Your grade will be at least: B") 
elif round((score_w + score_x + score_y + score_z, 1) (< 80) and (>= 70)): 
    print("Your grade will be at least: C") 
+2

把'round((score_w + score_x + score_y + score_z,1)'放在一個變量中,所以你不必一直重複它,然後重寫你的'if's來使用這個變量。變得更清晰 – Ryan

回答

0

我寫了一個快速的函數,我自己做了你想要的。

這裏是什麼是你的代碼錯誤:

  1. 您需要正確地縮進它。

  2. 這種比較是錯誤的: (< 90)和(> = 80))

你應該有類似這樣的東西(注:這是一個Python 2碼)

def totals(score_w, score_x, score_y, score_z): 
    rnd = round(score_w + score_x + score_y + score_z) 
    print "Overall percentage is: " + str(rnd) 
    if rnd >= 90.0: 
     print "A" 
    elif rnd < 90 and rnd >= 80: 
     print "B" 
    elif rnd < 80 and rnd >= 70: 
     print "C" 
3

你不能這樣做

round((score_w + score_x + score_y + score_z, 1) (< 90) and (>= 80)) 

,但你可以這樣做:

80 <= round(score_w + score_x + score_y + score_z, 1) < 90 

如果你想在這裏使用and運營商,你必須把它寫這樣的:

round(score_w + score_x + score_y + score_z, 1) < 90 and round(score_w + score_x + score_y + score_z, 1) >= 80 

如所建議的,你應該把你的回合函數成變種,如:

grade = round(score_w + score_x + score_y + score_z, 1) 

然後代碼將是一個乾淨得多:

if 80 <= grade < 90: 

if 80 <= grade and grade < 90: 
+1

可能值得指出的是,其中一個比較不是真正的y必要。 'elif'負責跳過足夠高分的成績。所以第一個檢查可以是'grade> = 90',然後下一個可以是'grade> = 80',而不必再次確認'grade <90'。 – Blckknght

相關問題