簡體   English   中英

Python生日代碼的語法錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM