2
#Basically, for the input number given, return results that are smaller 
#than it compared with the list.

n = list(input('Please enter a Number:  '))
#map(int, str(n))
newlist = []
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

for n in a:
    if n > a:
        newlist.append(a)
        print(newlist)

Not asking for the solution but just asking how n can work as a list since when I run my code, it says:

" if n > a:
TypeError: '>' not supported between instances of 'int' and 'list' "

3 Answers 3

3

Here is what you need. You can use a list comprehension to filter a for required elements.

x = int(input('Please enter a Number:  '))
# Input 5 as an example

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

result = [i for i in a if i < x]

print(result)
# [1, 1, 2, 3]
Sign up to request clarification or add additional context in comments.

3 Comments

good solution but the question was "Not asking for the solution but just asking how n can work as a list" though.
@Tenflex, to be honest I didn't understand the user's attempt. On the one hand, n is a list from a string input. On the other, for n in a is using n to iterate elements of a. Sometimes providing another method enlightens the OP.
yeah the question isn't clear though. but using locality of variable in the snippet n int is more local than n list
2

Assuming you're using python3, the input() method returns a string.

So if your user inputs the value 42, input() will return "42", (type 'str').

Then you are converting your string into a list, so going on with the 42 example, you'll have list("42") == ["4", "2"], and you store this list in n.

Anyway, you're not using your variable n anymore:

The for loop that you wrote does not not refer to the n variable you previously created (see namespaces), but creates a new variable which will contain each number in your a list.

this means that you are comparing each number of the list to the whole list:

1 > [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1 > [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
2 > [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
3 > [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
...

In python3 this will raise a TypeError, and in python2 the behaviour won't be what you expect.

To have your code correctly working, you may take the user input and convert it in an integer, then compare this integer with each number inside your list and append the number to a new list if it is lower than the input integer:

myint = int(input('Please enter a Number:  '))
# type(myint) == int
newlist = []
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

# for each (n)umber in list (a):
for n in a:
    # if the provided number is higher than the actual number in list (a):
    if myint > n:
        newlist.append(n)
# print the new list once its fully filled
print(newlist)

2 Comments

Thank you for this. Very well explained. Glad I came to this site.
Accept the answer if you think It answer to your question
1

because a is a list and your n in the iteration is an integer value inside of the list a. for example in the zeroth iteration n = 1 but a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] and saying n > a doesn't make sense, does it?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.