首页 > 解决方案 > 增加一个变量并存储该值?Python

问题描述

我对 Python 非常陌生。我正在尝试做一个测验,如果正确,您是否获得积分或如果错误则不获得积分。

total_Points = 0

question_1 = input("What is Sweden's biggest island?")
answer1 = "Gotland"


if question_1 == answer1:
    print("Gj, you are correct")

    print("You got" + str(total_Points+int(+1)) + " points")

else :
    print("Wrong")

    print("You still got " + total_Points + " points")

question_2 = input("What country is west of Sweden?")

answer2 = "Norway"

if question_2 == answer2:

    print("Correct!")

    print("Gj, you now have " + str(total_Points+int(+1))+ " points")
else:
    print("Nope, ur wrong")
    print("You still gott" + total_Points + " points")

如果你在 question_1 和 question_2 中得到一个点,你如何存储这个值?那么它应该是2分。如果您在第三个问题上回答错误怎么办?它怎么知道你只有 2 分?

谢谢新手回答。

标签: pythonincrement

解决方案


total_Points = 0

question_1 = input("What is Sweden's biggest island?")
answer1 = "Gotland"


if question_1 == answer1:
    print("Gj, you are correct")
    total_Points = total_Points + 1
    print("You got", total_Points, " points")
else :
    print("Wrong")
    print("You still got ", total_Points, " points")

question_2 = input("What country is west of Sweden?")

answer2 = "Norway"

if question_2 == answer2:

    print("Correct!")
    total_Points = total_Points + 1
    print("Gj, you now have ", total_Points, " points")
else:
    print("Nope, ur wrong")
    print("You still gott", total_Points, " points")

由于您已将变量声明total_Points为 0,因此您可以1total_Points = total_Points + 1. 所以,现在,total_Points将拥有更新的价值。

简而言之,您也可以增加它的价值total_Points+=1


推荐阅读