python - 增加一个变量并存储该值?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 分?
谢谢新手回答。
解决方案
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
。
推荐阅读
- python - 如何获取多行字符串的某一行
- python - 姜戈。使用 Count 参数注释不起作用
- javascript - 如何使用 wasm-bindgen 调用任意先前定义的 JavaScript 函数 Rust?
- seaborn - 模式与分布图中看到的不同
- html - 在宽屏幕上显示标题及其下方的段落无法正常工作
- python - Pandoc Filter via Panflute not Working as Expected
- python - 为什么我不能将一个数组分配为另一个数组的列
- python - 类与字典作为变量容器来摆脱引号?
- crc32 - zlib crc32 结合端格式
- windows - SignTool 错误:WinVerifyTrust 返回错误:0x80096019