-1

Starting from the following piece of code:

green = "formed after combining blue and yellow".
orange = "formed after combining red and yellow".
colour = str(input("enter a colour"))
if colour == "green": 
   print(f "The colour {colour}, {green} ")
if colour == "orange": print(f "The colour {colour}, {green} ") 
   print(f "The colour {colour}, {orange} ")

How can I make it so that after the answer of how the colour would be formed is printed, the user is asked again for the variable "colour" or another imput that asks again if he wants to consult how to form another colour.

That the first time the user wants to know how green is formed and then wants to know how orange is formed, or brown, etc. and you can keep showing him the input until he answers NO or END.

At the end, there should be a message like this. If you don't want to know more colours, press such and such and the program ends.

I understand that you have to use a while but I can't find any satisfactory explanation on youtube.

1 Answers1

0
green = "formed after combining blue and yellow"
orange = "formed after combining red and yellow"
while True:
    color = input("enter a color: ")
    if color == "green":
        print(f"The color {color}, {green}")
    elif color == "orange":
        print(f"The color {color}, {orange}")
    choice = input("do you want to consult how to form another color: ")
    if choice == "yes":
        continue
    elif choice == "no":
        break
  • There's no need for `continue` at the end of a loop body. Loops automatically continue unless the condition fails or `break` is executed. – Barmar Jan 11 '22 at 16:44
  • Use `pass` if you don't want to do anything in the `if`. Or just leave it out entirely: `if choice == "no": break` – Barmar Jan 11 '22 at 16:55
  • 1
    Yes, I understand that. But your code does the same thing if they input `yes` or `blah`. – Barmar Jan 11 '22 at 17:02