Python 输入永远不会等于整数

标签 python

<分区>

我想插入一个数字,如果我输入 4 以外的任何数字,它会告诉我这是错误的,但如果它是假的,它会告诉我“你赢了,菜鸟。”。但是,当我插入 4 时,它告诉我它不正确。

x = input("Insert a numer: ")

while x != 4:
   print("incorrect")
    x =input("Insert another number: ")

if x == 4:
    print("gg you win, noob")

最佳答案

在 Python 3+ 中,input 返回一个字符串,4 不等于 '4'。您将必须修改为:

while x != '4':

或者使用 int,如果输入不是 int,请小心检查 ValueError

关于Python 输入永远不会等于整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39775273/

相关文章:

Python,我可以导入*而不导入类吗

python - 解释 cv2.imencode 结果

python - 根据正则表达式中的第一个匹配项(而不是源中的匹配项)优先考虑正则表达式中的 OR Pipe?

python - 用于在所有空格处拆分的正则表达式 Python

python - 我应该怎么称呼这个函数组合?

python - 什么是__pycache__?

python - 在 python 装饰器中引发异常是一个好的模式吗?

Python gettext : convert Unicode literals into text

python - 在终端中设置路径

python - 如何突出显示数据帧的两个不同列中的唯一数据值?