Python生日代码的语法错误

时间:2018-06-18 11:13:22

标签: python python-3.x

我正在尝试学习在Python(3.6.5)中使用这个简单的生日单行,但是我得到了SyntaxError:语法无效,有人可以帮我弄清楚我出错的地方:

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

谢谢!

4 个答案:

答案 0 :(得分:6)

您已将其标记为python 3.x.这是python 2.x代码。你需要

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

因为print是一个函数,而不是python 3中的语句。map在python 2中返回listnow返回iterator需要包含在list中才能打印值。

这也是略显旧式的python,现代的写作方式是

print(["happy birthday to {}".format('you' if x != 2 else 'dear name') for x in range(4)]) 

答案 1 :(得分:2)

你的代码在python 3.x上失败的原因已经被@FHTMitchell回答了,那就是说,这里有一个更简单的方法来实现你想要的python2.x / 3.x:

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

有很多方法可以完成这项任务,还有更多的例子:

  • 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])

但如果你问我,我最喜欢的那个,即使不是一个oneliner,也会做这样的事情:

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())

答案 2 :(得分:2)

因为我们正在玩游戏,所以我们可以找到不同的方法让猫皮肤。

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

PS。我不喜欢lambdas,他们很慢。

另一方面,感谢@FHTMitchell,我一直都知道list.insert()很慢但我不知道它太慢了。

>>> 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

圣洁的抽烟!

答案 3 :(得分: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!")