Reputation: 13
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.
Upvotes: 0
Views: 428
Reputation: 11
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()
Upvotes: 0
Reputation: 24518
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.
Upvotes: 2
Reputation: 436
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')
Upvotes: 1
Reputation: 93
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')
Upvotes: 1
Reputation:
In this case, break
is superfluous. Removing it will start the loop again each time.
Upvotes: 0