打印输出看起来像是长循环

时间:2018-11-08 12:37:01

标签: python

我目前正在尝试为某人做某事,我是Python的初学者(和一般的编程,除了在bash脚本方面有一定的经验,所以请放轻松)我遇到了一个问题循环中,输出显示为拉伸状态,我不知道为什么,这需要一些帮助(所有名称/个人资料都替换为_)

#!/usr/bin/python3

# To _ :)
import sys
import os
import codecs
import colorama
import time

colorama.init()

# No idea why this comes out correct...

row_1 = "                      ❤️           ❤"
row_2 = "                    ❤️ ️❤️       ❤️  ❤"
row_3 = "                  ❤️      ❤️   ❤️      ❤"
row_4 = "                ❤️        ️❤️ ❤️          ❤"
row_5 = "              ❤️️           ️    ️          ❤"
row_6 = "                ❤️       For _____        ❤️"
row_7 = "                  ❤️     From ____      ❤"
row_8 = "                    ❤️                ❤"
row_9 = "                      ❤️            ❤"
row_10 = "                        ❤️        ❤"
row_11 = "                          ❤️    ❤"
row_12 = "                           ️ ❤️"
heart_list = [ row_1, row_2, row_3, row_4, row_5, row_6, row_7, row_8, row_9, row_10, row_11, row_12 ]

for weird in heart_list:
    print(colorama.Fore.RED + weird)
msg = """

    _________________________

"""
print(colorama.Style.RESET_ALL)
print("Type the number beside the thing you want to do, then press enter.")
Start_Menu = "1. My message \n" \
             "2. Heart (color) \n" \
             "3. Heart \n" \
             "4. Exit \n"
while True:
    print(Start_Menu)
    selection = input()

    if selection == "1":
        print(colorama.Fore.MAGENTA + msg)
        print(colorama.Style.RESET_ALL)

    if selection == "2":
        print("Not done yet")
        break
    if selection == "3":
        for whatever in heart_list:
            print(colorama.Fore.RED + whatever)
        print(colorama.Style.RESET_ALL)
    if selection == "4":
        print("___")
        exit()

# Time for the colorful heart
os.system("clear")
while True:
    time.sleep(1)
    red = False
    green = False
    yellow = False
    blue = False
    magenta = False
    cyan = False
    color_list = [ red, green, yellow, blue, magenta, cyan ]
    count = 0
    for current in heart_list:
        count += 1
    for current_2 in color_list:
        if all(color_list)  == True: # If everything in list is true...
            red = False
            green = False
            yellow = False
            blue = False
            magenta = False
            cyan = False
        if red == False: # False = not yet present
            print(colorama.Fore.RED)
            red = True
            break
        if green == False:
            print(colorama.Fore.GREEN)
            green = True
            break
        if yellow == False:
            print(colorama.Fore.YELLOW)
            yellow = True
            break
        if blue == False:
            print(colorama.Fore.BLUE)
            blue = True
            break
        if magenta == False:
            print(colorama.Fore.MAGENTA)
            magenta = True
            break
        if cyan == False:
            print(colorama.Fore.CYAN)
            cyan = True
    print(current)
    print(colorama.Style.RESET_ALL)
    if count == 12:
        os.system("clear")

https://pypi.org/project/colorama/(colorama,我正在使用的库)

这是在底部循环中的输出结果:

https://pastebin.com/(同样,在我的终端机中,一旦到达“发件人_”行,它就会停止使用任何颜色,如果您能告诉我为什么也会发生这种情况,那会很棒)

作为参考,这是在顶部附近的for循环中打印时的样子:

https://pastebin.com/aQwGnhZ8(在输出到我的终端时,一切似乎都是直截了当的,如果有人也可以解释这一点,那也很好,但正如您所看到的那样,它并不复杂)

我正在尝试使心脏变色(例如,如果第1行是蓝色,第2行是红色,第3行是绿色。下一个循环将是第1行绿色,第2行蓝色,第3行)红色等)。

预先感谢

1 个答案:

答案 0 :(得分:0)

这是因为print在其输出后添加了换行符(例如,按Enter键)。当您打印一行的颜色时,它正在打印一个换行符,这意味着您在行之间有一个额外的换行符。

您需要使用end关键字参数来禁止换行符。

print(colorama.Fore.MAGENTA, end="")

How can I suppress the newline after a print statement?