ITPub博客

首页 > IT职业 > IT生活 > 用Python写一棵圣诞树

用Python写一棵圣诞树

IT生活 作者:xduu 时间:2021-09-09 10:39:16 0 删除 编辑

效果如图:
图片描述

图片描述
Python源代码:

from random import randint 
from time import sleep
import colorama
from colorama import Fore, Back, Style
colorama.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 off
D:
cd D:Tree
:start 
python Tree.py
goto start

所以一共是两个文件:
图片描述

环境参数:
Python == 3.6.1

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2195/viewspace-2824597/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    84
  • 访问量
    1000