pythonpython-3.xlistappendempty-list

Appending and printing list


Ive tried to make a list in Python 3 where it appends and prints out all the numbers thats less than the userinput, but when the user types in 90 or more the list goes empty. Why?

x = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
num = int(input('Please enter a randome number: '))
a = 0
y = []
while (a < 11):
     if (x[a] < num):
    y.append(x[a])
#   print (x[a])
    a = a + 1
else:
    print (y)
    break

Solution

  • Here, this is much cleaner, proper indentation, and doesn't rely on sorted list:

    x = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
    num = int(input('Please enter a random number: '))
    
    y = []
    for item in x:
        if item < num:
            y.append(item)
    
    print(y)