简体   繁体   English

Python-颜色混合分配

[英]Python - Color Mixing Assignment

I'm learning Python through a class that I'm taking. 我正在通过我上的课来学习Python。 One of my assignments required that I create a system that asks a user for 2 primary colors then tells them what secondary color would be the result of putting them together. 我的一项任务要求我创建一个系统,该系统要求用户提供2种原色,然后告诉他们将它们组合在一起会产生什么副色。 When I run the code that I've pasted below, after asking for the 2nd input (2nd primary color) it circles back to the beginning again. 当我运行下面粘贴的代码时,在请求第二个输入(第二个原色)后,它再次回到起点。 I need help figuring out where I went wrong. 我需要帮助找出我哪里出了问题。

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

I'm not sure that I understood what your problem is. 我不确定我是否了解您的问题。

If I got you right, those steps should solve your problem: 如果我说对了,那么这些步骤应该可以解决您的问题:

  1. Instead of if primary_color2.lower() not in Primary_Colors: try while primary_color2.lower() not in Primary_Colors: . if primary_color2.lower() not in Primary_Colors:不是if primary_color2.lower() not in Primary_Colors:尝试if primary_color2.lower() not in Primary_Colors: while primary_color2.lower() not in Primary_Colors:
  2. You need to reference the input method to the right variable, such as Primary_Colors = input() . 您需要将输入法引用到正确的变量,例如Primary_Colors = input()
  3. Note that the last 'elif' block of code is not tabbed right, so it will print your result only when the 3rd condition is True. 请注意,最后一个“ elif”代码块的标签不正确,因此仅当第三个条件为True时,它才会输出结果。
  4. Consider adding a bool variable to determine if the process was successful, and if it's ok to print the result. 考虑添加一个bool变量来确定该过程是否成功以及是否可以打印结果。

The full code: 完整代码:

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

Bonus: you could try and except before any comprehension in order to prevent the main While loop. 奖励:您可以尝试除任何理解之前的和,以防止主While循环。

Unindent the print statements after the last elif, otherwise these are nested within that elif and will only be run if that elif condition is true: 取消最后一个elif之后的print语句的缩进,否则这些语句嵌套在该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

nice program! 不错的程序! You had two 'major' problems: the input was not that stable (by this I mean that if the user was to input something line Yellow, although the color yellow was in Primary_Colors[] the program would not identify it) so I lowercased all the inputs and changed your list of primary colors. 您有两个“主要”问题:输入不是那么稳定(这意味着如果用户要输入黄色行,尽管黄色位于Primary_Colors []中,程序将无法识别它),所以我将所有字母都小写输入并更改了原色列表。 Your other problem was that you had the printed results indented inside an if statement so it would not print for every iteration. 另一个问题是打印结果在if语句中缩进,因此不会在每次迭代中都打印出来。 You also included a 'break' that didn't let the results for a green combination to be printed. 您还包括了一个“中断”,该中断不允许打印绿色组合的结果。 Here is the whole fixed code for the program: 这是程序的整个固定代码:

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

Hope this was helpful to you :) 希望这对您有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM