I'm learning Python through a class that I'm taking. 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. 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:
if primary_color2.lower() not in Primary_Colors:
try while primary_color2.lower() not in Primary_Colors:
. Primary_Colors = input()
. 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.
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:
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. Your other problem was that you had the printed results indented inside an if statement so it would not print for every iteration. 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 :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.