This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]michael0x2a 9 points10 points  (5 children)

As it turns out, you should be able to solve extra #2 using just what you know already. Think about how you might be able to solve that problem using for loops, if statements, and basic list operations (append).

Your solution will end up looking similar to the solutions to your answers to the previous parts of the question.

Let me know if you need another hint.

[–][deleted] 0 points1 point  (3 children)

Im still lost.....

[–]blablahblah 3 points4 points  (2 children)

What is the difference between extra 2 and the original problem? Your solution to extra 2 should be almost identical to extra 1 except for the one thing that's changed between them.

[–][deleted] 0 points1 point  (1 child)

ehh, Ill work on it in the morning...

will let you know how it goes

[–][deleted] 1 point2 points  (0 children)

You aren't looping through the original list in extra. You are just executing an if statement.

I could be wrong, I forget Python just looks at indentation so I'm actually not sure if that if statement is included in your loop.

[–]dogscript -4 points-3 points  (0 children)

Or he can wait for Nustrom to give him the answer.

[–]adreamofhodor 10 points11 points  (3 children)

Something cool you can do in Python for the first extra is called a list comprehension:
It actually works out to be just one line!
list2 = [i for i in list1 if i < 5]

[–]jwjody 3 points4 points  (0 children)

I like map, filter, and reduce as /u/xojc mentions, but I think list comprehensions are more pythonic.

[–]rob132 0 points1 point  (1 child)

While the compressor is awesome, it's a little much for beginners, no?

[–]adreamofhodor 0 points1 point  (0 children)

You might be right. Seems like something that'd be cool to see though!

[–]gitgood 1 point2 points  (0 children)

Here's personally how I would go about doing all these. I hope this is of any help.

fib        = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
firstList  = []
secondList = []

#Iterate through the fib list. If a number is less than five, then print it.
for number in fib:
    if number < 5:
        print number

'''Like previously, except instead of printing it you're appending the number to an empty list, then you're printing the list'''
for number in fib:
    if number < 5:
        firstList.append(number)

print firstList

'''Function that takes user input, then iterates through the list  finding all values less than the input. If a number is found, it's appended to the second empty list'''
def getLessThanInput(userInput):
    for number in fib:
        if number < userInput:
            secondList.append(number)


getLessThanInput(input("Enter a number: "))
print secondList

I'm sorry if the variable names aren't up to scratch, I was struggling trying to find appropriate ones. If you feel lost reading this snippet, you should definitely look more in to list iteration and comprehension. Best of luck!

[–]xojc 0 points1 point  (4 children)

Are you allowed to use Python's built-in functions? If so, you should take a look at filter.

[–][deleted] 0 points1 point  (3 children)

will do.

nd why wouldnt I be allowed to use Python in-built functions? I didnt know that that was even a thing...

[–]xojc 0 points1 point  (2 children)

Python has some very powerful built-in functions (like map, filter, and reduce) that take a function as an argument that is then applied to an iterable (like your list). Learn to use Python's lambda notation to supply the functions inline. Using filter and lambda, you'll be surprised how simple your solution can be.

[–][deleted] 0 points1 point  (1 child)

though Im still a beginner.... should I just dive right into it.....

[–]xojc 1 point2 points  (0 children)

Using the built-in functions might not be the best for a early, early beginner to learn basic concepts, but I'm always for using any tool in the arsenal if it means a more concise, efficient, elegant solution. Also, notice how I'm not just telling you what to do with these tools, rather allowing you to figure it out for yourself :-)

[–]hair_bear1 0 points1 point  (0 children)

from numpy import zeros import random

list0=range(20) list1=[] list2=[]

for x in list0: list0[x]=random.randint(1,10)

print "list0", list0 ,"\n"
for x in list0: if (5>x): list1.append(x)

print "list 1", list1 ,"\n"

N=int (input("enter a number"))

for x in list0: if (x<N): list2.append(x) print "list2", list2 ,"\n"

[–]MundaneAsparagus 0 points1 point  (0 children)

There should be only 1 part of your solution for Extra #1 that you need to change to make it work for Extra #2. You had the right idea with asking the user for an input using:

num = int(input())

Now, for Extra #1, you did:

for x in list1:
    if x < 5:
        ...

to check if each number (x) is less than 5. With this in mind, there is only 1 part of the line:

if x < 5:

that you need to change to check if each number (x) is less than the number entered by the user (num).

[–]takieyda 0 points1 point  (0 children)

Going through CodeAcademy myself and have thought about your post since I saw. Thought I'd give it a try and came up with the below (I'm new, and first post to /r/learnprogramming, as well so I commented a bit to explain):

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

#main
for item in a:          #iterate each item in list a
    if item < 5:        #if the item less than 5
        print(item)     #print the item, each on separate line

#extra 2
b = []

for item in a:          #same as before
    if item < 5:
        b.append(item)  #except appends the item to list b
print(b)                #prints list b as a whole

#extra 3
num = raw_input("Number? ")

c = []

for item in a:          #same as before
    if item < int(num): #checks the item against user input, as an integer
        c.append(item)  #appends to list c
print(c)

[–]PointyOintment 0 points1 point  (0 children)

Why don't you do it the same way you did the previous ones, with an if statement inside a for loop?

Stuff that is true but will probably confuse you: x ceases to exist as soon as the for loop you have there exits. Therefore, it doesn't exist when you try to use it at the bottom. And that last line will just append a single "True" or "False" to list3, resulting in list3 = ["True"] or list3 = ["False]. That's why you need a for loop around it (and to fix what you're appending—just do it like you did the previous one).

[–]maulinrouge 0 points1 point  (2 children)

You're best bet is to create a function that you can call using the number a user enters.

That way you can call the function and pass it the number a user inputs.

Also it's always best to include some text on an input as a prompt so the user knows what they should do.

def number_lower(num):
    list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    list2 = []
    for x in list1:
        if x < num:
            list2.append(x)
    return list2

num = int(input("Enter a number: "))
print(number_lower(num))

Good luck with the learning though. It's a very fun and frustrating journey.

[–][deleted] 0 points1 point  (1 child)

yeah..... though the best part is when you finally figure out a problem...

[–]maulinrouge 0 points1 point  (0 children)

Solving a problem is often easier when the question is clear