Python's equivalent of && (logical
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?
你会想要and
而不是&&
。
Python uses and
and or
conditionals.
ie
if foo == 'abc' and bar == 'bac' or zoo == '123':
# do something
Two comments:
and
and or
for logical operations in Python. 下一篇: Python的等同于&&(逻辑