[英]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.