views:

453

answers:

5

Here's my code:

# F. front_back
# Consider dividing a string into two halves.
# If the length is even, the front and back halves are the same length.
# If the length is odd, we'll say that the extra char goes in the front half.
# e.g. 'abcde', the front half is 'abc', the back half 'de'.
# Given 2 strings, a and b, return a string of the form
#  a-front + b-front + a-back + b-back
def front_back(a, b):
  # +++your code here+++
  if len(a) % 2 == 0 && len(b) % 2 == 0:
    return a[:(len(a)/2)] + b[:(len(b)/2)] + a[(len(a)/2):] + b[(len(b)/2):] 
  else:
    #todo! Not yet done. :P
  return

I'm getting an error in the IF conditional. What am I doing wrong?

Edit: I meant no arrogance here. Someone edited my question title to make it sound douchy. I was genuinely confused about what to use, didn't think 'and' would be a keyword. Please don't downvote as other newbies might be confused about it as well.

+14  A: 

You would want and instead of &&.

ChristopheD
+1  A: 

Python uses and and or conditionals.

i.e.

if foo == 'abc' and bar == 'bac' or zoo == '123'
  # do something
David Titarenco
Don't forget that python also has not (well, and !)
inspectorG4dget
@David, fix that C++ comment to Python one :)
bgbg
haha durr, my fault :>
David Titarenco
A: 

I am a bit of a newbie with Python myself... You should definitely take a look into http://diveintopython.org/.

Here is the and-or chapter.

It's one of the better written "introduction books" I've read, doesn't assume you are a master programmer (but doesn't assume you know nothing about programming either)

Matthew Doyle
Note that while it's a decent hands-on book, it's full of hacks and somewhat ugly code, like the "and-or trick" on that page (which is no longer needed).
Max Shawabkeh
is there a better resource to get general 'non-hack' code? I am trying to learn it for django
Matthew Doyle
The MIT OCW Python course is quite decent: http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-00Fall-2008/CourseHome/index.htm. Think Like a Computer Scientist is good as well: http://openbookproject.net/thinkcs/python/english2e/
Max Shawabkeh
Have you seen this one? http://code.google.com/edu/languages/google-python-class/
Joe Holloway
@Joe: That looks great, although a little too short for my taste.
Max Shawabkeh
+1  A: 

Two comments:

  • Use and and or for logical operations in Python.
  • Use 4 spaces to indent instead of 2. You will thank yourself later because your code will look pretty much the same as everyone else's code. See PEP 8 for more details.
istruble
A: 

In python, like in C, boolean and bitwise operations are separate, but the boolean operators are spelled differently.

The boolean ones are spelled and and or. They have two advantages: they can work for any type of operands (types/classes that define how to be interpreted in a boolean context, i.e. have a __nonzero__ special method), and they take a shortcut if they can. For example, 1 or lengthy_operation() will never call the lengthy operation.

The bitwise operators (&/and, |/or and ^/xor) have the same meaning as in C, but they work only on integers (or other classes convertable to integer), and they don't short-circuit. Although not advisable, should you prefer to use the bitwise operators as boolean, make sure you use parentheses around the boolean terms, and that they are indeed boolean:

if (len(a) % 2 == 0) & (len(b) % 2 == 0):
ΤΖΩΤΖΙΟΥ