龙空技术网

圣诞节啊,用Python写一棵圣诞树出来

千锋IT教育 1096

前言:

如今大家对“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 == 3.6.1

标签: #python 批处理文件