0
col1 = input('Please input the first color: ')
col2 = input('Please input the second color: ')

while True:

    if (col1 == 'red' and col2 == 'blue')  or (col1 == 'blue' and col2 == 'red'):
        print('purple')
    elif (col1 == 'red' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'red'):
        print('orange')
    elif (col1 == 'blue' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'blue'):
        print('green')
    if col1 not in ['red', 'blue', 'yellow']:
        print('Invalid')
        break 

I'm trying to let the user answers 'red', 'blue' or 'yellow'. If they do not answer in that list, the program will print ('invalid') and start the loop by asking the user again which 2 colors to mix.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 2
    I would expect your input commands to be inside the loop, and you should break out of the loop when you get valid input, not when you get invalid input. – khelwood Nov 14 '20 at 08:35
  • Sure I would do that. It hasn't solved the problem yet. There's only one that is close to what I want for the output. Thank you for letting me know :) – Rothvitou Long Nov 16 '20 at 23:35
  • If it doesn’t solve your problem, let the author know and they may update the answer accordingly. Also, if it is close enough, you may be able to figure out the rest. – Abhijit Sarkar Nov 16 '20 at 23:58

5 Answers5

2
colors = { 
    ("blue", "red") : "purple",
    ("red", "yellow") : "orange",
    ("blue", "yellow") : "green",
}

def mix(color1, color2):
    c = tuple(sorted((color1, color2)))
    return colors.get(c)
    
c1 = input("First color: ")
c2 = input("Second color: ")

while not (m := mix(c1, c2)):
    print("Invalid")
    c1 = input("First color: ")
    c2 = input("Second color: ")
    
print(m)

First color: a

Second color: b

Invalid

First color: blue

Second color: red

purple

No need for multiple if-else checks.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
  • 1
    Nice answer. Just note that `:=` only works for python >= 3.8 [link](https://stackoverflow.com/questions/26000198/what-does-colon-equal-in-python-mean) – big_in_japan Nov 14 '20 at 09:31
  • Thank you! I might have forgot to mention that I want the program to print "Invalid" if the First Color is not the required mix. Great code tho it teaches me new way to improve :D – Rothvitou Long Nov 15 '20 at 19:00
1

I am assuming that you want to exit the loop when the inputs are valid and the loop will run again until the inputs are valid.

To do that modify your code slightly like this. What happens here is that, as you run the program it goes straight into the loop and asks the user input. After input it evaluates and if they are valid it breaks and moves out of the loop. But if the input is not valid it will go to the next iteration and will ask the input again.


while True:
    col1 = input('Please input the first color: ')
    col2 = input('Please input the second color: ')
    if (col1 == 'red' and col2 == 'blue')  or (col1 == 'blue' and col2 == 'red'):
        print('purple')
        break
    elif (col1 == 'red' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'red'):
        print('orange')
        break
    elif (col1 == 'blue' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'blue'):
        print('green')
        break
    if col1 not in ['red', 'blue', 'yellow']:
        print('Invalid')
1

This should work:

VALID_ARGUMETNS = ['red', 'blue', 'yellow']


def user_input():
    col1 = input('Please input the first color: ')
    col2 = input('Please input the second color: ')
    return col1, col2


if __name__ == '__main__':
    col1, col2 = user_input()

    while col1 not in VALID_ARGUMETNS or col2 not in VALID_ARGUMETNS:
        print('Invalid, try again:')
        col1, col2 = user_input()

    if (col1 == 'red' and col2 == 'blue') or (col1 == 'blue' and col2 == 'red'):
        print('purple')
    elif (col1 == 'red' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'red'):
        print('orange')
    elif (col1 == 'blue' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'blue'):
        print('green')
Saggi Bashari
  • 366
  • 4
  • 13
0

In this case, break is superfluous. Removing it will start the loop again each time.

0

Using loops in this program is a bit complex, it is right but it makes the program more complex comparing what it does. A simple code like this should work -

def col():
    col1 = input('Please input the first color: ')
    col2 = input('Please input the second color: ')
    a=['red','blue','yellow']
    if col1 in a and col2 in a:
        if (col1=='red' and col2=='blue')  or (col1=='blue' and col2=='red'):
            print('purple')
        elif (col1=='red' and col2=='yellow') or (col1=='yellow' and col2=='red'):
            print('orange')
        elif (col1=='blue' and col2=='yellow') or (col1=='yellow' and col2=='blue'):
            print('green')
    else:
        print('Invalid')
        col()
col()
Vansh
  • 11
  • 4