Python 输入永远不等于整数 [英] Python input never equals an integer

查看:53
本文介绍了Python 输入永远不等于整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to insert a number and if I put any number other than 4 it will tell me it's wrong, but if it's false it will tell me "gg you win, noob.". However when I insert 4, it tells me it's incorrect.

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'.您必须修改为:

In Python 3+, input returns a string, and 4 does not equal '4'. You will have to amend to:

while x != '4':

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

or alternatively use int, being careful to check for a ValueError if the input is not an int.

这篇关于Python 输入永远不等于整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆