Python-颜色混合分配

时间:2019-02-04 23:13:13

标签: python

我正在通过我上的课来学习Python。我的一项任务要求我创建一个系统,该系统要求用户提供2种原色,然后告诉他们将它们组合在一起会产生什么副色。当我运行下面粘贴的代码时,在请求第二个输入(第二个原色)后,它再次回到起点。我需要帮助找出我哪里出了错。

map

3 个答案:

答案 0 :(得分:0)

我不确定我是否了解您的问题。

如果我说对了,那么这些步骤应该可以解决您的问题:

  1. 尝试使用if primary_color2.lower() not in Primary_Colors:而不是while primary_color2.lower() not in Primary_Colors:
  2. 您需要将输入法引用到正确的变量,例如Primary_Colors = input()
  3. 请注意,最后一个“ elif”代码块未正确制表,因此仅当第三个条件为True时,它才会打印结果。
  4. 考虑添加一个bool变量来确定该过程是否成功,以及是否可以打印结果。

完整代码:

isSuccessful = False
while True:
    try:
        Primary_Colors = ["red" , "blue" , "Yellow"]
        Secondary_Colors = ["orange" , "purple" , "green"]
        print("---------------------------------------------------------------------------------------\n")
        print("Note: The colors red, blue, and yellow are known as primary colors because they cannot")
        print("be made by mixing other colors. When you mix primary colors, you get a secondary color. \n")
        print("---------------------------------------------------------------------------------------\n\n")
        primary_color1 = input("Please enter your first primary color: ")
        while primary_color1.lower() not in Primary_Colors:
            primary_color1 = input(Please enter a valid primary color [red, blue, or yellow]. Press any key to start over.\n)

        primary_color2 = input("Please enter your second primary color: ")
        while primary_color2.lower() not in Primary_Colors:
            primary_color1 = input(Please enter a valid primary color [red, blue, or yellow]. Press any key to start over.\n)

        if primary_color1.lower() == primary_color2.lower():
            primary_color2.lower() = input("You have already selected this primary color. Press any key to start over.")

        print("\n------------------------------------------------------------------------------------")
        if ((primary_color1.lower() == Primary_Colors[0] and primary_color2.lower() == Primary_Colors[2]) or (primary_color1.lower() == Primary_Colors[2] and primary_color2.lower() == Primary_Colors[0])):
            secondaryColor = Secondary_Colors[0]
            isSuccessful = True
        elif ((primary_color1.lower() == Primary_Colors[1] and primary_color2.lower() == Primary_Colors[0]) or (primary_color1.lower() == Primary_Colors[0] and primary_color2.lower() == Primary_Colors[1])):
            secondaryColor = Secondary_Colors[1]
            isSuccessful = True
        elif ((primary_color1.lower() == Primary_Colors[1] and primary_color2.lower() == Primary_Colors[2]) or (primary_color1.lower() == Primary_Colors[2] and primary_color2.lower() == Primary_Colors[1])):
            secondaryColor = Secondary_Colors[2]
            isSuccessful = True
        if isSuccessful:
            print("")
            print("When you mix ({0:s}) and ({1:s}) you get ({2:s})." .format(primary_color1.capitalize(), primary_color2.capitalize(), secondaryColor.capitalize()))
            print("\n-------------------------------------------------------------------------------\n")
            break
    except ValueError:
        print("please enter a valid primary color.")
        continue

奖金:您可以在理解之前尝试除,以防止主While循环。

答案 1 :(得分:0)

在最后一个elif之后取消打印语句的缩进,否则这些语句嵌套在该elif中,并且仅在该elif条件为true时才运行:

    if ((primary_color1.lower() == Primary_Colors[0] and primary_color2.lower() == Primary_Colors[2]) or (primary_color1.lower() == Primary_Colors[2] and primary_color2.lower() == Primary_Colors[0])):
        secondaryColor = Secondary_Colors[0]
    elif ((primary_color1.lower() == Primary_Colors[1] and primary_color2.lower() == Primary_Colors[0]) or (primary_color1.lower() == Primary_Colors[0] and primary_color2.lower() == Primary_Colors[1])):
        secondaryColor = Secondary_Colors[1]
    elif ((primary_color1.lower() == Primary_Colors[1] and primary_color2.lower() == Primary_Colors[2]) or (primary_color1.lower() == Primary_Colors[2] and primary_color2.lower() == Primary_Colors[1])):
        secondaryColor = Secondary_Colors[2]
    print("")
    print("When you mix ({0:s}) and ({1:s}) you get ({2:s})." .format(primary_color1.capitalize(), primary_color2.capitalize(), secondaryColor.capitalize()))
    print("\n-------------------------------------------------------------------------------\n")
    break

答案 2 :(得分:0)

好程序!您有两个“主要”问题:输入不是那么稳定(这意味着如果用户要输入黄色行,尽管黄色位于Primary_Colors []中,程序将无法识别它),所以我将所有字母都小写输入并更改了原色列表。另一个问题是打印结果在if语句中缩进,因此不会在每次迭代中都打印出来。您还包括了一个“中断”,该中断不允许打印绿色组合的结果。这是该程序的全部固定代码:

while True:
try:
    Primary_Colors = ["red" , "blue" , "yellow"]
    Secondary_Colors = ["orange" , "purple" , "green"]
    print("---------------------------------------------------------------------------------------\n")
    print("Note: The colors red, blue, and yellow are known as primary colors because they cannot")
    print("be made by mixing other colors. When you mix primary colors, you get a secondary color. \n")
    print("---------------------------------------------------------------------------------------\n\n")
    primary_color1 = input("Please enter your first primary color: ").lower()
    if primary_color1.lower() not in Primary_Colors:
        print("Please enter a valid primary color [red, blue, or yellow]. Press any key to start over.\n")
        input()
        continue
    primary_color2 = input("Please enter your second primary color: ").lower()
    if primary_color2.lower() not in Primary_Colors:
        print("Please enter a valid primary color. Press any key to start over.")
        input()
        continue
    if primary_color1.lower() == primary_color2.lower():
        print("You have already selected this primary color. Press any key to start over.")
        input()
        continue
    print("\n------------------------------------------------------------------------------------")
    if ((primary_color1.lower() == Primary_Colors[0] and primary_color2.lower() == Primary_Colors[2]) or (primary_color1.lower() == Primary_Colors[2] and primary_color2.lower() == Primary_Colors[0])):
        secondaryColor = Secondary_Colors[0]
    elif ((primary_color1.lower() == Primary_Colors[1] and primary_color2.lower() == Primary_Colors[0]) or (primary_color1.lower() == Primary_Colors[0] and primary_color2.lower() == Primary_Colors[1])):
        secondaryColor = Secondary_Colors[1]
    elif ((primary_color1.lower() == Primary_Colors[1] and primary_color2.lower() == Primary_Colors[2]) or (primary_color1.lower() == Primary_Colors[2] and primary_color2.lower() == Primary_Colors[1])):
        secondaryColor = Secondary_Colors[2]
    print("")
    print("When you mix ({0:s}) and ({1:s}) you get ({2:s}).".format(primary_color1.capitalize(),
                                                                     primary_color2.capitalize(),
                                                                     secondaryColor.capitalize()))
    print("\n-------------------------------------------------------------------------------\n")
except ValueError:
    print("please enter a valid primary color.")
    continue

希望这对您有所帮助:)