编译Python程序,但也输出颜色



我用python为我的孩子们制作了一个非常基本的数学练习程序,其他一些家长也询问过使用它的问题。唯一的问题是,当我试图编译它时,它以终端形式输出,并且没有我在PyCharm中添加的任何颜色。有没有一种方法可以让颜色正常运行,这样如果我与他人共享,它的运行方式与PyCharm中的我相似?下面是完整的代码。由于学校已经关闭,我想制作一个简单的.exe供人们下载和共享。

import os
import json
import random
import sys
import time
from IPython.display import clear_output

class bc:
head = '33[95m'
blue = '33[94m'
green = '33[92m'
yellow = '33[93m'
red = '33[91m'
end = '33[0m'
bold = '33[1m'
under = '33[4m'

def cl():
clear_output()

pinfo = {}
pinfoall = []
seta = []
setb = []
setc = []
setd = []

def log():
global pinfo
global pinfoall
login = input("Please Enter Your Player Name: ")
if os.path.isfile("./mathdata.json") and os.stat("./mathdata.json").st_size != 0:
datas = open("./mathdata.json", "r+")
pinfoall = json.loads(datas.read())
datas.close()
pfound = False
for item in pinfoall:
if item["Player"] == login:
cl()
print(bc.blue + "n Welcome Back to Math Masters, " + bc.green + login + bc.blue + "!" + bc.end)
print(bc.end + "Type your answer to answer a question. To quit and save your progress type 'q'.")
time.sleep(4)
pinfo = item
pfound = True
if pfound:
pinfoall.pop(pinfoall.index(pinfo))
if not pfound:
a = 0
while a == 0:
new = input("No player named " + login + " found. Create new player?  ('y' for yes, 'n' to retype login)")
if new == "y":
pinfo = {"Player": login, "Level": 1, "XP": 0, "HP": 10, "HPMax": 10}
print(bc.blue + "n Welcome to Math Masters, " + bc.green + login + bc.blue + "!" + bc.end)
print(bc.end + "Type your answer to answer a question. To quit and save your progress type 'q'.")
time.sleep(4)
a += 1
elif new == "n":
log()
a += 1
else:
print(bc.red + bc.bold + "Must type 'y' or 'n'!")
else:
pinfo = {"Player": login, "Level": 1, "XP": 0, "HP": 10, "HPMax": 10}
print(bc.blue + "n Welcome to Math Masters, " + bc.green + login + bc.blue + "!" + bc.end)
print(bc.end + "Type your answer to answer a question. To quit and save your progress type 'q'.")
time.sleep(6)
pinfoall = []
datas = open("./mathdata.json", "w+")
datas.write(json.dumps(pinfoall))
datas.close()

log()

class pl:
name = pinfo["Player"]
lv = pinfo["Level"]
xp = pinfo["XP"]
hp = pinfo["HP"]
mhp = pinfo["HPMax"]

def nums_a_s(level):
global seta
global setb
seta = []
setb = []
for i in range(0, 5+level):
seta.insert(len(seta), i)
setb.insert(len(setb), i)

def nums_m_s(level):
global setc
global setd
setc = []
setd = []
if level > 14:
setc = [0, 1, 2]
setd = [0, 1, 2]
if level > 17:
setc = [0, 1, 2, 3]
setd = [0, 1, 2, 3]
if level > 20:
setc = [0, 1, 2, 3, 4]
setd = [0, 1, 2, 3, 4]
if level > 23:
setc = [0, 1, 2, 3, 4, 5]
setd = [0, 1, 2, 3, 4, 5]
if level > 26:
setc = [0, 1, 2, 3, 4, 5, 6]
setd = [0, 1, 2, 3, 4, 5, 6]
if level > 29:
setc = [0, 1, 2, 3, 4, 5, 6, 7]
setd = [0, 1, 2, 3, 4, 5, 6, 7]
if level > 32:
setc = [0, 1, 2, 3, 4, 5, 6, 7, 8]
setd = [0, 1, 2, 3, 4, 5, 6, 7, 8]
if level > 35:
setc = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
setd = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
if level > 38:
setc = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
setd = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
if level > 41:
setc = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
setd = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
if level > 44:
setc = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
setd = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

def equation(level):
if level < 15:
ops = [0, 1]
else:
ops = [0, 1, 2, 3]
num3 = random.choice(setc)
num4 = random.choice(setd)
op = random.choice(ops)
num1 = random.choice(seta)
num2 = random.choice(setb)
if op == 0:
eq = "What is "+  str(num1) + " + " + str(num2) + "?   " + " (Addition)n"
ans = str(num1 + num2)
if op == 1:
if level < 10:
if num1 < num2:
a = num1
num1 = num2
num2 = a
eq = "What is " + str(num1) + " - " + str(num2) + "?   " + " (Subtraction)n"
ans = str(num1 - num2)
if op == 2:
eq = "What is " + str(num3) + " X " + str(num4) + "?   " + " (Multiplication)n"
ans = str(num1 * num2)
if op == 3:
a = -1
b = -1
while a not in setc:
denom = random.choice(setd[1:])
numer = random.choice(setc)
if numer % denom == 0:
a = numer
b = denom
eq = "What is " + str(a) + " ÷ " + str(b) + "?   " + " (Division)n"
ans = str(a/b)
return eq, ans

def correct():
print(bc.green + "CORRECT! " + bc.blue + "Good job, " + bc.green + pl.name + bc.blue + "!" + bc.end)
if pl.xp < 9:
pl.xp += 1
print(bc.blue + "You gained " + bc.green + "1" + bc.blue + " experience point!" + bc.end)
time.sleep(1)
else:
print(bc.blue + "You gained a" + bc.red + " LEVEL" + bc.blue + "! You are now level " +
bc.green + str(pl.lv + 1) + bc.blue + "!nYour Hit Points increased by " + bc.green + "5" + bc.blue +
" and have been fully restored!" + bc.end)
pl.lv += 1
pl.mhp += 5
pl.hp = pl.mhp
pl.xp = 0
time.sleep(5)
os.remove("mathdata.json")
pinfo["Level"] = pl.lv
pinfo["HP"] = pl.hp
pinfo["HPMax"] = pl.mhp
pinfo["XP"] = pl.xp
datas = open("mathdata.json", "w+")
datas.write(json.dumps(pinfoall + [pinfo]))
datas.close()

def wrong():
pl.hp -= 5
print(bc.red + "WRONG!" + bc.blue + " Better luck next question, " + bc.green + pl.name + bc.blue + "! You took " +
bc.red + "5" + bc.blue + " Hit Points of damage." + bc.end)
if pl.hp < 1:
if pl.lv > 1:
print(bc.blue + "You" + bc.red + " DIED" + bc.blue + "! You are now level " +
bc.green + str(pl.lv - 1) + bc.blue + "!nYour Max Hit Points decreased by " + bc.green + "5" + bc.blue +
"." + bc.end)
pl.lv -= 1
pl.mhp -= 5
pl.hp = pl.mhp
pl.xp = 0
time.sleep(4)
else:
print(bc.blue + "You " + bc.red + "DIED" + bc.blue +
"! You are already at the minimum level!nYou have respawned with your base stats." + bc.end)
pl.hp = 10
pl.xp = 0
time.sleep(4)
os.remove("mathdata.json")
pinfo["Level"] = pl.lv
pinfo["HP"] = pl.hp
pinfo["HPMax"] = pl.mhp
pinfo["XP"] = pl.xp
datas = open("mathdata.json", "w+")
datas.write(json.dumps(pinfoall + [pinfo]))
datas.close()

def qmath():
qv = input(bc.end + "Are you sure you want to quit? (y/n)")
if qv == "y":
os.remove("mathdata.json")
pinfo["Level"] = pl.lv
pinfo["HP"] = pl.hp
pinfo["HPMax"] = pl.mhp
pinfo["XP"] = pl.xp
datas = open("mathdata.json", "w+")
datas.write(json.dumps(pinfoall + [pinfo]))
datas.close()
print(bc.blue + "n Thanks for playing, " + bc.green + pl.name + bc.blue + "! See you next time, Math Master!" + bc.end)
quit()
elif qv == "n":
run()
else:
print("You must type 'y' or 'n'")
qmath()

def run():
cl()
nums_a_s(pl.lv)
if pl.lv > 14:
nums_m_s(pl.lv)
print(bc.end + "       ------- " + bc.blue + "Player: " + bc.green + pl.name + bc.end + " ------- " + bc.blue +
"Level: " + bc.green + str(pl.lv) + bc.end + " ------- " + bc.blue + "HP: " + bc.green + str(pl.hp) +
bc.end + "/" + bc.green + str(pl.mhp) + bc.end + " ------- " + bc.blue + "XP: " + bc.green + str(pl.xp) +
bc.end + "/" + bc.green + "10" + bc.end + " ------- nn")
print(bc.red + "Here is your question! n" + bc.end)
q, a = equation(pl.lv)
b = input(q)
if b == "q":
qmath()
if str(b) == str(a):
correct()
else:
wrong()
time.sleep(2)
run()

run()

窗口中的颜色有点棘手。pycharm中的shell必须支持颜色更改ANSI代码,但windows的cmd不支持。我建议你看看https://github.com/tartley/colorama

此外,还有一个线程可能对您有用:更改外壳文本颜色(Windows(

最新更新