為孩子們準備的 Python Mad Libs

好吧,我想出了一個 Python 的想法,讓我 6 歲的女兒非常著迷。 瘋狂的利伯斯! 它基本上只是一系列融入故事的文本提示。 Python 中的 f-string 對她來說非常直觀,而且她很快就能夠幫助我進行故障排除。 這是她通過填寫自己的故事、大聲朗讀和笑掉她的屁股來娛樂自己的好方法......

這是腳本:

 1from datetime import datetime
 2
 3a = input("Enter a name: ")
 4a1 = input(f"Is {a} a boy or girl: ")
 5a2 = "He" if a1 == "boy" else "She"
 6b = input("Enter an adjective: ")
 7c = input("Enter an animal (including Human): ")
 8d = input("Enter a food: ")
 9e = input("Enter a verb: ")
10f = input("Enter another name: ")
11g = input("Enter a place: ")
12h = input("Enter another name: ")
13i = input("Enter an action (past tense): ")
14j = input("Enter an action (past tense): ")
15k = input("Enter a food: ")
16l = input("Enter an action (past tense): ")
17m = input("Enter a noun: ")
18
19non_particular = "an" if b[0].upper() in ["A", "E", "I", "O", "U"] else "a"
20
21story = f" \
22Once upon a time, there was {non_particular} {b} {c} named {a}. \
23{a} loved to eat {d}. {a2} also liked to {e} \
24with {f}, and watch TV.  \
25\
26One day, {a} went to the {g} and found {h}. They {i} and {j}. \
27They also ate {k} and {l} on the {m}. \
28\
29The End. \
30\
31"
32
33print(story)
34
35save_story = input("Save story? Y/N: ")
36if save_story.upper() == "Y":
37	outF = open(f"""story_{datetime.now().strftime("%Y%m%d_%H%M%S")}.txt""", "w")
38	outF.write(story)
39	outF.close()

這是一個示例輸出:

Once upon a time, there was a cute tigee named Moofy. Moofy loved to eat ice cream. She also liked to fart with Nellie, and watch TV. One day, Moofy went to the the bed and found Daddy. They played piano and burped. They also ate pizza and slept on the remote control. The End.

翻譯: