[英]Syntax error with Python birthday code
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:我正在尝试学习在 Python (3.6.5) 中使用这个简单的生日单行代码,但收到 SyntaxError: invalid syntax,有人能帮我找出我哪里出错了:
print map(lambda x: "Happy Birthday to " + ("you" if x != 2 else "dear Name"),range(4))
Thanks!谢谢!
You've tagged this as python 3.x.您已将其标记为 python 3.x。 This is python 2.x code.这是 python 2.x 代码。 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.因为print
是一个函数,而不是 python 3 中的语句。 map
返回了 python 2 中的一个list
,但现在返回一个迭代器,该迭代器需要包含在list
中以打印值。
This is also slightly old-style python, a modern way of writing this would be这也是有点老式的python,现代的写法是
print(["happy birthday to {}".format('you' if x != 2 else 'dear name') for x in range(4)])
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:你的代码在 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)])
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:但如果你问我,我最喜欢的人,即使不是 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())
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.我不喜欢 lambda,它们很慢。
On another note, thanks @FHTMitchell, I've always known list.insert()
was slow but i had no idea it was SOO slow.另一方面,感谢@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
Holy smokes!圣烟!
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!")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.