亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

所以我在1周前開始編程,我有點困惑為什么我的程序沒有減去1,當任何問題的答案不正確時

所以我在1周前開始編程,我有點困惑為什么我的程序沒有減去1,當任何問題的答案不正確時

jeck貓 2022-09-13 17:23:36
我無法從這個python程序中的分數中減去一個。當我運行代碼時,它顯示從0到4的答案,但不是負數,我希望答案為負,如果答案太多是錯誤的。代碼如下:***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 += 1else:print('incorrect')    score -= 1# second questionprint("question 2: what is my age? ")ans2 = input()if ans2 == '19':    print('correct')    score += 1else:print('incorrect')score -= 1print("question 3: what is my fathers name? ")ans3 = input()# third questionif ans3 == 'john':    print('correct')    score += 1else:print('incorrect')score -= 1**# fourth question**print("question 4: what is my mothers name? ")ans4 = input()if ans4 == 'Emily':    print('correct')    score += 1else:print('incorrect')score -= 1print ('your score is', score ) # answer can be in negative***
查看完整描述

3 回答

?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

這實際上甚至不應該運行。

else:print('incorrect')
    score -= 1

您需要有一個單行 else 語句,或者將 else 語句的所有代碼放在下一行并正確縮進它。蟒蛇是關于空白的。

以下內容應該可以解決您的問題。

else:    print('incorrect')
    score -= 1


查看完整回答
反對 回復 2022-09-13
?
搖曳的薔薇

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

如果您在腳本中嘗試這樣做,結果可能更符合您的預期。


查看完整回答
反對 回復 2022-09-13
?
慕尼黑的夜晚無繁華

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


查看完整回答
反對 回復 2022-09-13
  • 3 回答
  • 0 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號