如何在python3中给用户输入

发布于 2025-01-12 09:06:05 字数 307 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱自由 2025-01-19 09:06:05

在代码中,第一行应更正为 n=int(input("enter the no:"))

python中,它将输入作为字符串。想象一下,如果您将输入指定为3。那么变量n将存储值“3”(而不是值3)。您应该知道 3 == "3"False

因此,当您获取input时,您应该将该字符串input转换为int。为此,我们使用 int() 方法。 int() 方法将指定值转换为整数

n=int(input("请输入编号:"))
检查(测试列表,n)

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

In your code the first line should be corrected as n=int(input("enter the no:")).

In python it takes the inputs as strings. Think if you are giving the input as 3. Then the variable n stores the value "3"( not the value 3 ). You should need to know 3 == "3" is False.

Therefore, when you are taking the input you should convert that string input to the int. To do that we use int() method. The int() method converts the specified value into an integer number

n=int(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
掩耳倾听 2025-01-19 09:06:05

您没有得到任何结果,因为 input() 函数始终返回一个字符串。因此,我们需要在将其传递到函数之前将其转换为整数,我使用 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)

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()).

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文