如何在python3中给用户输入
n=input("enter the no:")
check(test_list,n)
def check(test_list,n):
for i in test_list:
if(i==n):
print("yes in a list")
continue
else:
continue
我编写了简单的代码来检查是否否。是否存在于列表中,但是在接受用户输入时,在提供输入值 n 后我没有得到任何结果。
为什么会这样?
n=input("enter the no:")
check(test_list,n)
def check(test_list,n):
for i in test_list:
if(i==n):
print("yes in a list")
continue
else:
continue
I had written the simple code to check if a no. exists in a list or not, but while taking user input I"m not getting any results after providing input value n.
Why is so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在代码中,第一行应更正为
n=int(input("enter the no:"))
。在
python
中,它将输入
作为字符串
。想象一下,如果您将输入
指定为3。那么变量n
将存储值“3”(而不是值3)。您应该知道3 == "3"
是False
。因此,当您获取
input
时,您应该将该字符串input
转换为int
。为此,我们使用int()
方法。int()
方法将指定值转换为整数n=int(input("请输入编号:"))
检查(测试列表,n)
In your code the first line should be corrected as
n=int(input("enter the no:"))
.In
python
it takes theinputs
asstrings
. Think if you are giving theinput
as 3. Then the variablen
stores the value "3"( not the value 3 ). You should need to know3 == "3"
isFalse
.Therefore, when you are taking the
input
you should convert that stringinput
to theint
. To do that we useint()
method. Theint()
method converts the specified value into an integer numbern=int(input("enter the no:"))
check(test_list,n)
您没有得到任何结果,因为 input() 函数始终返回一个字符串。因此,我们需要在将其传递到函数之前将其转换为整数,我使用
n=int(input())
进行了操作。You were not getting any results because the input() function always returns a string. For this reason, we need to convert it to an Integer before passing it on into the function, and I did with
n=int(input())
.