[英]Syntax error with Python birthday code
我正在尝试学习在 Python (3.6.5) 中使用这个简单的生日单行代码,但收到 SyntaxError: invalid syntax,有人能帮我找出我哪里出错了:
print map(lambda x: "Happy Birthday to " + ("you" if x != 2 else "dear Name"),range(4))
谢谢!
您已将其标记为 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 中的一个list
,但现在返回一个迭代器,该迭代器需要包含在list
中以打印值。
这也是有点老式的python,现代的写法是
print(["happy birthday to {}".format('you' if x != 2 else 'dear name') for x in range(4)])
你的代码在 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())
既然我们在玩,让我们找到不同的方法来给猫剥皮。
placeholders = ['to you'] * 4
placeholders[2] = 'dear name' # placeholders.insert(2, 'dear name')
print([f'Happy birth {placeholder}' for placeholder in placeholders])
附: 我不喜欢 lambda,它们很慢。
另一方面,感谢@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
圣烟!
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.