简体   繁体   English

如何在python3中给用户输入

[英]How to give user input in 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

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.是否存在于列表中,但是在接受用户输入时,我在提供输入值 n 后没有得到任何结果。

Why is so?为什么会这样?

In your code the first line should be corrected as n=int(input("enter the no:")) .在您的代码中,第一行应更正为n=int(input("enter the no:"))

In python it takes the inputs as strings .python中,它将inputs作为strings Think if you are giving the input as 3. Then the variable n stores the value "3"( not the value 3 ).想一想如果您将input作为 3。那么变量n存储值“3”(不是值 3)。 You should need to know 3 == "3" is False .您应该需要知道3 == "3"False

Therefore, when you are taking the input you should convert that string input to the int .因此,当您接受input时,您应该将该字符串input转换为int To do that we use int() method.为此,我们使用int()方法。 The int() method converts the specified value into an integer number int()方法将指定值转换为integer数字

n=int(input("enter the no:")) check(test_list,n) n=int(input("输入编号:")) 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

You were not getting any results because the input() function always returns a string.您没有得到任何结果,因为 input() function 总是返回一个字符串。 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()) .出于这个原因,我们需要先将其转换为 Integer,然后再将其传递到 function,我使用n=int(input())进行了处理。

n=int(input("enter the no:"))

def check(test_list,n):
    for i in test_list:
        if(i==n):
             print("yes in a list")
             continue
        else:
            continue

check((1,2,3),n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM