为了账号安全,请及时绑定邮箱和手机立即绑定

所以我在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 关注
  • 100 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号