Python的 - 混色分配

问题描述 投票:0回答:3

我通过我正在学习类的Python。我的一个任务的需要,我创建要求用户2个原色然后告诉他们什么辅助色会是把它们放在一起的结果的系统。当我跑,我已经粘贴下面,询问2号输入后的代码(第二原色)IT圈回来再开始。我需要帮助搞清楚我错在哪里。

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: ")
        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: ")
        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")
            break
    except ValueError:
        print("please enter a valid primary color.")
        continue
python
3个回答
0
投票

取消缩进最后的elif后的报表打印,否则这些都是嵌套在ELIF内,如果该elif的条件为真将只运行:

    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

0
投票

我不知道,我明白你的问题是什么。

如果我给你的权利,这些步骤应该解决您的问题:

  1. 相反if primary_color2.lower() not in Primary_Colors:的尝试while primary_color2.lower() not in Primary_Colors:
  2. 您需要引用输入法权变量,如Primary_Colors = input()
  3. 需要注意的是代码的最后“的elif”块不对的标签,所以它会打印你的结果只有当第三个条件为真。
  4. 考虑增加一个布尔变量来确定过程是成功的,如果它是确定以打印结果。

完整的代码:

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循环。


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

希望这是对您有所帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.