3 回答

TA貢獻1921條經驗 獲得超9個贊
這實際上甚至不應該運行。
else:print('incorrect') score -= 1
您需要有一個單行 else 語句,或者將 else 語句的所有代碼放在下一行并正確縮進它。蟒蛇是關于空白的。
以下內容應該可以解決您的問題。
else: print('incorrect') score -= 1

TA貢獻1793條經驗 獲得超6個贊
無論答案如何,都將調用增量。例如
# second question
print("question 2: what is my age? ")
ans2 = input()
if ans2 == '19':
print('correct')
score += 1
else:print('incorrect')
score -= 1
如果答案是正確的,那么分數將遞增,但無論如何,分數也會降低。需要正確縮進,如下所示score -=1
# second question
print("question 2: what is my age? ")
ans2 = input()
if ans2 == '19':
print('correct')
score += 1
else:
print('incorrect')
score -= 1
如果您在腳本中嘗試這樣做,結果可能更符合您的預期。

TA貢獻1864條經驗 獲得超6個贊
該程序幾乎沒問題。如果我回答所有問題不正確,我將獲得負分。最后一行是錯誤的,因為您無法將積分器與字符串連接起來。此外,您需要在“else”語句下正確縮進內容。這將是完成的程序:
print("Hello and welcome to my quiz! ")
score = 0
# this the question i want it to add 1 if answer is correct and subtract 1 if answer is incorrect
print("question 1: what is my name? ")
ans1 = input()
if ans1 == 'mark':
print('correct')
score += 1
else:
print('incorrect')
score -= 1
# second question
print("question 2: what is my age? ")
ans2 = input()
if ans2 == '19':
print('correct')
score += 1
else:
print('incorrect')
score -= 1
print("question 3: what is my fathers name? ")
ans3 = input()
# third question
if ans3 == 'john':
print('correct')
score += 1
else:
print('incorrect')
score -= 1
# fourth question**
print("question 4: what is my mothers name? ")
ans4 = input()
if ans4 == 'Emily':
print('correct')
print('your score is ' + str(score) )
添加回答
舉報