python - 即使答案错误,Python 的分数也会增加

标签 python python-3.x

<分区>

所以我目前正在为我正在制作的问答游戏开发计分器。

score = 0

bolivia = str(input("Name a capital of Bolivia."))

if (bolivia == "La Paz" or "Sucre"):

    print ("Correct")

    score = score + 50

    print (score)

else:

    print ("You/re incorrect")

input ("Press enter to exit;)")

问题是,如果我得到错误的答案,它会打印出这个:

Correct
50
Press enter to exit;)

我不知道我做错了什么。感谢任何反馈。

最佳答案

声明

if (bolivia == "La Paz" or "Sucre"):

有如下两条语句:

1) 玻利维亚 == "拉巴斯"

2) “苏克雷”

后面的语句 "Sucre" 是一个字符串,它是 Truthy value , 因此 if 语句作为一个整体将通过,因为它们与 or 关键字连接在一起。

您可能想要评估的是

if bolivia == "La Paz" or bolivia == "Sucre":

或者考虑 in 关键字:

if bolivia in ("La Paz", "Sucre"):

关于python - 即使答案错误,Python 的分数也会增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50322529/

相关文章:

python - 使用python从div中抓取h3

python - 初始化带索引和不带索引的 pandas 数据框,列会产生不同的结果

list - 最大的质因数 Python

python - 列表附加在循环问题中

python - 避免数据帧中的重复数据连接/合并/连接

python - 旋转多索引数据

python - 如何在 python 代码中找到 txt 文件中的每个 'x'?

python - 将 pandas 数据框写入 MySQL

python - 为什么pip3安装包却无法导入呢?

python - 如何使用列表中的数字作为 Python 字典的键?