99 Problems (Chapter 14)...python Course


#1

https://www.codecademy.com/courses/python-beginner-BxUFN/3/4

@dra1bry2dev3 . This one was a big challenge because we had never seen the function "answer" and when you fumble around and see what comes of the code you supplied the answers are backwards.

What you need is this:

for if:
replace the _____ with answer > 5 : [ include only the words answer > 5 : ]
for elif:
replace the _____ with answer < 5 :

disregard the proof in the black box from your script that returns

-1
1
0

I know those results are backwards from what you are expecting of

1
-1
0.

I spent hours, yes hours with help from other users and a Code Academy staffer and still don't know why the proof comes out -1, 1, 0 and quite honestly I think it would require quite a bit of explaining.

If there is anyone who can explain why the proof for the function if when "answer > 5 :" and result is scripted as 1, but proof's to a -1 I would LOVE to hear it.

Many thanks to codecademy for such an awesome place to learn. I'm a network engineer by way of Environmental Science and Forestry by way of Medical Technology [yes 3 major career changes]. The only "code" I know is IOS.

kind regards,
Tammy


#2

Well first off, just in general explanation. You need answer because that is the parameter used in your function. You use the parameter in your if else and elif to determine if the answer given is greater or lesser than 5 then give back a certain answer on the module screen. And if it fulfills neither of those choices it is to give back a certain answer as well. Those "answers" are -1, 1 and 0. You get these because that number is returned if the statement is attributed to is fulfilled. It would seem your question if I am understanding you correctly and I qoute,

is the above and if so it is a simple answer. Your returned values are as such. If you change for example, answer > 5 to return -2 instead of 1 it will return -2 instead and -2 will show up on the console screen if the if statement is fufilled to which the answer > 5 and return -2 is attributed.

I dearly hope, if I wrongly assumed your question, this does not come off snarky or high and mighty. I hope this has helped and if not hopefully someone else can find use for this answer. :smiley:

Also a moderator or super user might want to change this discussion to Community Corner Bar instead as it is misplaced.


#3

This exercise is a precursor to a sorting algorithm. Taking numbers into account, given a list of them, how will we determine which ones to swap and which ones to leave alone? By comparing two values at a time.

numbers = [3,7,4,5,9,7,5,2,4,1,5,7,8]

There are a lot of ways we can perform the swapping, the most common of which is bubble sort. In any method, we will need a comparison function to determine when a swap is called for.

def compare(a, b):
    if a > b: return 1
    if a < b: return -1
    return 0

A return of 1 means a comes after b in a sorted list, so the two values are swapped. The other returns result in no swap.

Now say in our lesson code we are building two lists, one with values greater than 5, and one with values less than 5. When the return value is 1, we append the value to the list of greater than 5, and when it is -1 we append the value to the list of less than 5.

>>> def less_than_greater_than_5(x):
    return -1 if x < 5 else 1 if x > 5 else 0

>>> less_than_greater_than_5(5)
0
>>> less_than_greater_than_5(3)
-1
>>> less_than_greater_than_5(7)
1
>>>

Don't let my ternary expression throw you off. It is essentially an if..elif..else written in one expression rather than statement form.

Now given our list,

>>> def less_than_greater_than_5(x):
    return -1 if x < 5 else 1 if x > 5 else 0

>>> def create_less_greater_lists(n):
    less_than = []
    greater_than = []
    for m in n:
        if less_than_greater_than_5(m) == 1:
            greater_than.append(m)
        elif less_than_greater_than_5(m) == -1:
            less_than.append(m)
        else: continue
    for m in less_than:
        n.remove(m)
    for m in greater_than:
        n.remove(m)
    return less_than, n, greater_than

>>> numbers = [3,7,4,5,9,7,5,2,4,1,5,7,8]
>>> a, b, c = create_less_greater_lists(numbers)
>>> a, b, c
([3, 4, 2, 4, 1], [5, 5, 5], [7, 9, 7, 7, 8])
>>>

While the above is not the most practical method, it serves to demonstrate the underlying logic of this lesson.


#4

Thank you soo soo very much for your explanations. My challenge in understanding all of this is simply this. Why is the script [function] resulting in -1, 1, 0 vs 1, -1, 0

Def greater_less_equal_5(answer):
if answer > 5 :
return 1
elif answer < 5 :
return -1
else
return 0

print greater_less_equal_5(answer)(4)
print greater_less_equal_5(answer)(5)
print greater_less_equal_5(answer)(6)

So the proof of the formula being [for a lack of better technical terms] when written as is above results in a -1, 1,0. NOT 1, -1, 0 as I would expect.

Disregard formatting [caps etc] I have not figured out how to copy out of the python editor and the formatting looks as it should until I hit the save button for this post.

If it's logic I'm missing I will reread your posts and wait for the "ah ha."

many thank,
Tammy


#5

Thank you. In reference to your statement:

"If you change for example, answer > 5 to return -2 instead of 1 it will return -2 instead and -2 will show up on the console screen if the if statement is fufilled to which the answer > 5 and return -2 is attributed."

my example is "answer > 5 return 1" and I am getting -1.

That simply is my challenge in understanding this context.

many thanks,
and no not snarky one bit,

regards,
Tammy


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.