Home > database >  How to give user input in python3
How to give user input in python3

Time:03-08

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?

CodePudding user response:

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

CodePudding user response:

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)
  • Related