增加变量并存储值?蟒蛇

时间:2020-05-16 16:26:09

标签: python increment

我是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")

如果您在问题_1和问题_2中都得到了分数,那么如何存储值?然后应该是2分。如果您在第三个问题上回答错误,该怎么办?它怎么知道您只有2分?

感谢您回答新手。

4 个答案:

答案 0 :(得分:0)

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,因此可以添加1并像total_Points = total_Points + 1一样重新分配给它。因此,现在total_Points将具有更新后的值。

简而言之,您也可以像total_Points+=1一样增加它的值。

答案 1 :(得分:0)

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 += 1
    print("You got" + str(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 += 1 
    print("Gj, you now have " + str(total_Points)+ " points")
else:
    print("Nope, ur wrong")
    print("You still gott" + total_Points + " points")

您将添加到变量total_Points中以保持计数。

答案 2 :(得分:0)

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+=1;

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

将向您展示方法

答案 3 :(得分:0)

只需在所有正确答案的total_Points语句下增加if
例如:

if (correct answer):
    total_Points = total_Points + 1