views:

148

answers:

4

The question i'm working on asks "Given the variables name1 and name2 , write a fragment of code that assigns the larger of their associated values to first" ('first' in bold letters)

(NOTE: "larger" here means alphabetically larger, not "longer". Thus, "mouse" is larger than "elephant" because "mouse" comes later in the dictionary than "elephant"!)<--- as it adds under the question

So far i have this code "= (name1, name2)" and the error message is telling me that i may need to insert something before and after the = sign. What could that be?

+5  A: 

I'd suggest reading something like the Python Tutorial to get started.

Amber
A: 

= is not a function. It is an operator You need a single lvalue (left value) for that operator such as

lvalue = rvalue1 + rvalue2 ...

What you need is a function that returns the max such as mac(pram1, param2).

Hint: max

And then use max as an rvalue (right value).

thyrgle
+1  A: 

OK, let start with the beginning:

To assign some value to the variable name1 you write something like:

 name1 = 'mouse'

Name1 is the name of the variable, 'mouse' is the content you want to store in it.

Now if you want to copy the content of name1 to first you would do:

first = name1

Now name1 and first both contains the string mouse.

Now the remaining problem is to make a choice. Which is the greater content of two variables name1 and name2 ? Comparizon operators like < will give you the answer.

Finally, depending of which is the larger you'll copy either name1 or name2 to first. You should have a look at the if construct to do that.

(You could also use the max function as another poster suggested, but I believe that's not the goal of the exercise)

kriss
+2  A: 

Quit being so stingy with the code, people! Here, I think your professor should appreciate this approach:

#!/usr/bin/env python

largest = False

def findlargesthelper(list):
    global largest
    for str in list:
        if largest == False or str > largest:
            largest = str

def findlargest(list):
    global largest
    findlargesthelper(list)
    result = largest
    # Don't forget to reset it for the next time around!
    largest = None
    return result

first = findlargest(('mouse', 'elephant'))
print first

You only really want to use globals when iterating string lists, and "False" as a default comprehension value is pretty standard Python. Please do let us know how the assignment goes for you, would you?

Just Some Guy
+1 but I'm tempted to downvote because you neglected to have `findlargesthelper` end with the line `findlargest.func_globals['result'] = largest`, and save yourself the needless line `result = largest` in `find_largest`.
aaronasterling
Egad! I'm such a newbie sometimes. Also, needs more lambda.
Just Some Guy
Warning for real newbies: this answer is obviously a joke, you shouldn't take it seriously (and showing globals and spaghetti code like this one to your teacher is probably a good way not to graduate).
kriss
While I agree, Kriss, I think that showing globals and spaghetti code like this one to your teacher is an excellent way to learn that you have to figure this stuff out for yourself. Asking to be spoonfed will never benefit you; even if you get the right answers, you've deprived yourself of the learning process.
Just Some Guy
@Just Some Guy: I've mixed feeling about that. While trying to figure out some stuff will give me good habits (think before asking), to see how someone more experienced than I will write some piece of code can be more enlightenment than half figuring a poor answer by myself. Some years ago I helped a student engineer with it's C homeworks, before that he had tried to solve that stuff by himself and failed for 2 years. After showing him how things could be done he finally gratuated and said he learned more coding skill figuring out my stuff than through his teacher lessons.
kriss