porpo
Posts: 2
Joined: Sun Oct 06, 2019 12:45 pm

What will happen if the user choose the same colour for both the message and the background.

Tue Oct 08, 2019 12:31 pm

Code: Select all

from sense_hat import SenseHat
from time import sleep
from random import randint

sense = SenseHat()
r = (255,0,0)
b = (0 ,0, 255)
g = (0, 255, 0)

while True:
            red = int(input("Please enter the value of the red color for message"))
            if  0 < red < 255:
                print("That is a correct value")
                r = (red,0,0)
            elif red > 255:
                print("That is not a valid number, Try again...")
                continue
              
            green = int(input("Please enter the value of the blue color for message"))
            if 0 < green < 255:
                print("That is a correct value")
                r = (red,green,0)
            elif green > 255:
                print("That is not a valid number, Try again...")
                continue
             
            blue = int(input("Please enter the value of the green color for message"))     
            if 0 < blue < 255:
                print("That is a correct value")
                r = (red,green,blue)
            elif blue > 255:
               print("That is not a valid number, Try again...")
               continue

            try:  
               scroll_speed = float(input("Please enter scroll speed")) #enter an int or float to break program
               break
            except ValueError:
               print("Oops!  That was no valid number.  Try again...")
      
          
sense.show_message("Studying",text_colour = r, back_colour = b,scroll_speed = 0.05)

sense.clear()

How would you resolve this in your design to let the user try again and again until the user enter correct input?

hippy
Posts: 7459
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: What will happen if the user choose the same colour for both the message and the background.

Tue Oct 08, 2019 1:01 pm

Something like ...

Code: Select all

def GetColor(prompt):
 ... set r, g and b here ...
 return (r,g,b)

while True:
  text_color = GetColor("Enter text color")
  back_color = GetColor("Enter background color")
  if text_color == back_color:
    print("Text and background colors must not be the same")
  else:
    break

print("text_color =", text_color)
print("back_color =", back_color)
But you might want a more clever comparison of colours because black (0,0,0) will not be the same as not quite black (0,0,1) though to all intents and purposes that would appear as 'black on black'.

Return to “Python”