天天看點

聖誕節啊,用Python寫一棵聖誕樹出來

實圖:

聖誕節啊,用Python寫一棵聖誕樹出來

效果如圖:哈哈哈哈哈。。。

聖誕節啊,用Python寫一棵聖誕樹出來
聖誕節啊,用Python寫一棵聖誕樹出來

Python源代碼:

from random import randint from time import sleepimport coloramafrom colorama import Fore, Back, Stylecolorama.init()rnd2 = randint(1,60)def gentree():for x in range(1,30,2):rnd1 = randint(1,rnd2)if x == 1:ch = "$"elif rnd1 % 4 == 0:ch = "o"elif rnd1 % 3 == 0:ch = "j"elif rnd1 % 5 == 0:ch = "o"elif rnd1 % 7 == 0:ch = "j"else:ch ="*"if ch == "$":print(Fore.RED +"{:^33}".format(ch x))elif ch == "o":print(Fore.RED +"{:^33}".format(ch x))elif ch == "j":print(Fore.YELLOW +"{:^33}".format(ch x))else:print(Fore.GREEN +"{:^33}".format(ch x))print("{:^33}".format('|||'))print("{:^33}".format('|||')) print("{:^33}".format(' Merry_christmas '))sleep(.24)gentree()

我試了一下,用IDLE運作這段代碼效果不咋滴。為了讓聖誕樹一直在閃,我決定用批處理的方式,寫一個bat腳本來運作py檔案。

bat代碼:

@echo offD:cd D:Tree:start python Tree.pygoto start

是以一共是兩個檔案:

聖誕節啊,用Python寫一棵聖誕樹出來

環境參數:

聖誕節啊,用Python寫一棵聖誕樹出來