我正在做这个"测验",我正在尝试像评分系统一样添加
#starting score
score = 0
if Question_1 in ("Cheetah", "CHEETAH", "cheetah"):
score =+ 1
else:
pass
if Question_2 in ("Sailfish", "SAILFISH", "sailfish"):
score =+ 1
else:
pass
if Question_3 in ("Peregrine Falcon", "peregrine falcon", "PEREGRINE FALCON", "pf"):
score =+ 1
else:
pass
if Question_4 in ("A decade", "Decade", "DECADE", "decade"):
score =+ 1
else:
pass
if Question_5 in ("Centimetre", "Centimetres", "CENTIMETRE", "CM", "cm", "centi", "centimetre"):
score =+ 1
else:
pass
#once all answers have been checked display result
if score == 0:
print("your result was 0/5, very bad!")
elif score == 1:
print("your result was 1/5, bad!")
elif score == 2:
print("your result was 2/5, mid!")
elif score == 3:
print("your result was 3/5, good!")
elif score == 4:
print("your result was 4/5, very good!!")
elif score == 5:
print("your result was 5/5, EXCELLENT!!")
else:
pass
当我测试我的代码时,如果我的分数是 0,它会输出为 0,但如果我的分数是 1、2、3、4 或 5,它会输出,就好像我只回答了一个正确的问题一样,有人知道为什么吗? 我也想知道是否有任何其他方法可以做分数系统,例如使用"while循环"?
您需要的运算符是+=
。=+
只是一个带有有符号正整数的=
。
将score =+ 1
更改为score += 1
。+=
和-=
用于递增和递减。=+
和=-
用于有符号整数。
>>> score = 0
>>> score += 1
>>> score
1
>>> score = -1
>>> score =+ 1
>>> score
1
为什么需要通过其他?当您想要跳过循环中的进一步执行时,将使用 pass。在您的代码片段中,我认为 pass 没有任何意义。
如果If-Else
语句不在循环中,则可以删除传递。如果使用If
,则不强制要求Else
。
score = 0
if Question_1 in ("Cheetah", "CHEETAH", "cheetah"):
score =+ 1
if Question_2 in ("Sailfish", "SAILFISH", "sailfish"):
score =+ 1
if Question_3 in ("Peregrine Falcon", "peregrine falcon", "PEREGRINE FALCON", "pf"):
score =+ 1
if Question_4 in ("A decade", "Decade", "DECADE", "decade"):
score =+ 1
if Question_5 in ("Centimetre", "Centimetres", "CENTIMETRE", "CM", "cm", "centi", "centimetre"):
score =+ 1