0

I am trying to learn to use this simple birthday one-liner in Python (3.6.5) but am getting SyntaxError: invalid syntax, can someone help me figure out where I've gone wrong:

print map(lambda x: "Happy Birthday to " + ("you" if x != 2 else "dear Name"),range(4))

Thanks!

2
  • 3
    if it's python3, add brackets to print() Commented Jun 18, 2018 at 11:14
  • 3
    @OmarEinea and wrap in list since map is an iterator in 3.x Commented Jun 18, 2018 at 11:15

4 Answers 4

6

You've tagged this as python 3.x. This is python 2.x code. You need

print(list(map(lambda x: "Happy Birthday to " + ("you" if x != 2 else "dear Name"),
               range(4))))

since print is a function, not a statement in python 3. Also map returned a list in python 2 but now returns an iterator which needs to be wrapped in list in order to print the values.


This is also slightly old-style python, a modern way of writing this would be

print(["happy birthday to {}".format('you' if x != 2 else 'dear name') for x in range(4)]) 
Sign up to request clarification or add additional context in comments.

Comments

2

The reason why your code fails on python 3.x as already been answered by @FHTMitchell answer, that said, here's a simpler way to achieve what you want in both python2.x/3.x:

print(["Happy Birthday to {}".format("you" if x!=2 else "dear Name") for x in range(4)])

There is a lot of ways to achieve this task, few more examples:

  • lst = ["Happy Birthday to you"]*4; lst[1] = lst[1].replace("you", "dear Name"); print(lst)

  • s1 = "Happy Birthday to you"; print([s1,s1.replace("you", "dear Name"),s1,s1])

But if you ask me, my favourite one, even if not a oneliner, would be doing something like this:

import textwrap

song = textwrap.dedent("""\
    Happy Birthday to you
    Happy Birthday to dear Name
    Happy Birthday to you
    Happy Birthday to you
""")

print(song.splitlines())

5 Comments

beat you to it ;)
posted at the same time than you, meh... giving you +1 for being that fast ;D
Na the best one liner would be: print([f'Happy birth to {placeholder}' for placeholder in ['you', 'you', 'dear name', 'you']]) its actually one line :P
Not an expert on English songs... but wouldn't be Happy birthday instead Happy birth? :D
2

Since we're playing lets find different ways to skin a cat.

placeholders = ['to you'] * 4
placeholders[2] = 'dear name'  # placeholders.insert(2, 'dear name')
print([f'Happy birth {placeholder}' for placeholder in placeholders])

ps. I don't like lambdas, they're slow.


On another note, thanks @FHTMitchell, I've always known list.insert() was slow but i had no idea it was SOO slow.

>>> import timeit
>>> timeit.timeit('a += ["something"]', setup='a = ["thing"]')
0.07153259999904549
>>> timeit.timeit('a[0] = "something"', setup='a = ["thing"]')
0.03036419999989448
>>> timeit.timeit('a.insert(0, "something")', setup='a = ["thing"]')
264.89795089999825

Holy smokes!

4 Comments

If you don't like lambda because it's slow, definitely don't use list.insert!
Your code won't be python2.x compatible... and the song says happy birthday, ie: http://www.1happybirthday.com/traditional_birthday_lyrics.php :'D
@BPL OP said 3.6.5 -- thats what I use, as far as im concerned, python without f-strings is beneath me, and i refuse to acknowledge its existence. Python was born at 3.6 :P edit: i fixed the song !!! :P
@Javier Buzzi Haha, fair enough, I hadn't seen the python3-x tag ;) , +1
0
from time import  sleep
def start():
    name="Birthday person"
    print("\t\ti i i")
    sleep(1)
    print("\t\ti i i")
    sleep(1)
    print("\t\ti i i")
    sleep(1)
    print("\t :------------------:")
    sleep(1)
    print("\t ▐█░█▌█▀▀█ █▀▀█ █▀▀█ █▌▄█")
    sleep(1)
    print("\t ▐█▀█▌█▄▄█ █▄▄█ █▄▄█ █▄██")
    sleep(1)
    print('\t ▐█░█▌█▌▐█ █▌░░ █▌░░ ░██')
    sleep(1)
    print('────────???.•°”˜˜”°•.???')
    sleep(1)
    print('▀██▀▀▄──???.•°”˜˜”°•.???')
    sleep(1)
    print('─██▄▀▄──???.•°”˜˜”°•.???')
    sleep(1)
    print('─██───█─▄──────▄──█───')
    sleep(1)
    print('─██───█─▄─▄─▄─▄█▄─█─▄─')
    sleep(1)
    print('▄██▄▄▀──█─█▀───█──█▀─█')
    sleep(1)
    print('────────█─█────█──█──█')
    sleep(1)
    print('██████║░░▄▄▄▄║▄▄║░▄║♥')
    sleep(1)
    print('███║░██║██║░█║██║░█║♥')
    sleep(1)
    print('███║░██║██║░█║██║░█║♥')
    sleep(1)
    print('███║░██║▀█▄▀█║▀█▄▀█║♥')
    sleep(1)
    print('███║░██║░░░░░░▄░░░█║♥')
    sleep(1)
    print('██████║░░░░░░░▀███▀')
    sleep(1)
    print('')
    sleep(1)
    print('')
    sleep(1)
    print('')


    sleep(2)
    print("A message for you.....")
    sleep(4)
    print("\tMAY GOD BLESS YOU AND YOUR FAMILY\n ALWAYS BE HAPPY AND GOOD HEALTH")
    sleep(5)
    print("one second......")
    print()
    sleep(.3)
    print('────▀██████─────▀─────────────█')
    sleep(.3)
    print('─────▌█───▀─█▄▀─█─▄▀▄─█▄▀█─▄▀▀█─▄▀▀')
    sleep(.3)
    print('─────▌███───█───█─█▀▀─█──█─█─▄█─▀▀█')
    sleep(.3)
    print('─────▌█─────▀───▀──▀▀─▀──▀──▀─▀─▀▀▀')
    sleep(.3)
    print('────▄██▄───█──█─────────────────────')
    sleep(.3)
    print('──────────▀█▀─█▄▀▄─▄▀▄')
    sleep(.3)
    print('───────────█──█──█─█▀▀')
    sleep(.3)
    print('───────────▀──▀──▀──▀▀')
    sleep(.3)
    print('──▄▄─▄▄──────▀██▀▀▄───────────█')
    sleep(.3)
    print('─█░█████──────▌█▄▀▄──▄▀▄─▄▀▀─▀█▀')
    sleep(.3)
    print('─▀█░███▀──────▌█───█─█▀▀─▀▀█──█')
    sleep(.3)
    print('───▀█▀────────▌█───█──▀▀─▀▀▀──▀')
    sleep(.3)
    print('─────────────▄██▄▄▀')

a=input("1.Type : 'MJ' \n 2.PRESS 'ENTER' \n (ELSE TYPE 'QUIT' TO QUIT)\n\n:" )
if (a == 'MJ'):
    start()
else:
    print("TAKE CARE BYE!")

1 Comment

While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. You can use the edit button to improve this answer to get more votes and reputation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.