[英]Python - Color Mixing Assignment
我正在通过我上的课来学习Python。 我的一项任务要求我创建一个系统,该系统要求用户提供2种原色,然后告诉他们将它们组合在一起会产生什么副色。 当我运行下面粘贴的代码时,在请求第二个输入(第二个原色)后,它再次回到起点。 我需要帮助找出我哪里出了问题。
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
我不确定我是否了解您的问题。
如果我说对了,那么这些步骤应该可以解决您的问题:
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:
。 Primary_Colors = input()
。 完整代码:
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循环。
取消最后一个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
不错的程序! 您有两个“主要”问题:输入不是那么稳定(这意味着如果用户要输入黄色行,尽管黄色位于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
希望这对您有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.