python - 编译Python程序,但也输出颜色吗?

标签 python colors compiler-errors pycharm exe

我为我的 children 编写了一个非常基本的python数学练习程序,其他一些 parent 也问过要使用它。唯一的问题是,当我尝试对其进行编译时,它会在终端中输出,并且没有我添加的任何颜色对我来说在PyCharm中都可以正常工作。如果我与他人共享色彩,是否可以使色彩正常运行,使其运行与在PyCharm中为我运行的色彩类似?完整的代码如下。由于学校已经关闭,我正在寻找一个供人们下载和共享的简单.exe。

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


class bc:
    head = '\033[95m'
    blue = '\033[94m'
    green = '\033[92m'
    yellow = '\033[93m'
    red = '\033[91m'
    end = '\033[0m'
    bold = '\033[1m'
    under = '\033[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 + " ------- \n\n")
    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

另外,此线程可能对您有用:
Changing Shell Text Color (Windows)

关于python - 编译Python程序,但也输出颜色吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61022837/

相关文章:

python - 根据多个其他列中特定范围内的日期的存在情况创建列

python - 在 Python 中关闭文件

javascript - 更改形状的颜色而不重新创建

java - 有人可以解释为什么对我的数组进行这些特定更改可以修复我的错误吗?

c++ - 错误 C2280:Class::Class(void):尝试引用已删除的函数

python - 如何计算具有重复元素的列表的排列(排列)

python - Pandas:从 dict 在 DataFrame 中创建命名列

c# - 如何更改 RichTextBox 的部分文本颜色

iphone - iPhone 上从 RGB 更改为 HSB?

c++ - 错误 C2259 : 'CDocument' : cannot instantiate abstract class