Python Forum

Full Version: what is missing in my while statement ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello friends, I'm new here.
I am still a beginner but I am quite familiar with loops and while statement and have used it many times.

I'm trying a simple exercise but the result only shows when the number I type exists in the list, otherwise I get an error which I don't understand :


list = [1,3,4,6,7]
number = int(input("Pick a number : "))
i=0
while list[i] != number :
    if i < len(list) :
        i = i+1
    else :
        print("your numeber has not been found")
print(f"the position of your number is {i}")
Could you please help me understand which part of my while statement is not correct ?

Thank you Smile
You can use the 'in' clause:
list = [1,3,4,6,7]

number = int(input("Pick a number : "))
if number in list:
    print(f"Number is in list at index: {list.index(number)}")
else:
    print("your numeber has not been found")
Your program crashes because you use list[i] before testing if i is in range.
Post the full traceback you get.
As a side note - don't use list as name.