Reputation: 41
I am making a calculator to turn RGB values into Hexidecimal numbers. as I was coding I realised I had written the same code three times to check user input for red, green, and blue. So I thought, why not use a function to check my variables for me!! Here is my code:
invalid_msg = 'Whoops looks like you have entered incorrect information'
def check_rgb(var):
while var > 255 or var < 0:
print invalid_msg
var = int(raw_input('Please enter a value between 0 and 255:'))
return var
def rgb_hex():
red = int(raw_input('Enter your value for red.'))
check_rgb(red)
green = int(raw_input('Enter your value for green.'))
while green > 255 or red < 0:
print invalid_msg
green = int(raw_input('Enter your value for green.'))
blue = int(raw_input('Enter your value for blue.'))
while blue > 255 or red < 0:
print invalid_msg
blue = int(raw_input('Enter your value for blue.'))
val = (red << 16) + (green << 8) + blue
print '%s' % (hex(val)[2:]).upper()
rgb_hex()
The issue is with redeclaring the variable. Right now this stores the value entered into the function to the variable 'var' not red.
Upvotes: 1
Views: 93
Reputation: 55499
As Davis Yoshida mentions, merely doing check_rgb(red)
doesn't change the original value of red
, since you forgot to save the value that check_rgb
returns.
I recommend expanding your helper function so that it gets the user input and validates it too. Here's an example that also makes sure that the input is a valid integer in the proper range. This code also shows an easier way to print the hex representation of the RGB value.
def get_color_byte(name):
while True:
s = raw_input('Enter a value from 0 to 255 for %s: ' % name)
try:
v = int(s)
if not 0 <= v <= 255:
raise ValueError('Out of range')
return v
except ValueError as e:
print e
r, g, b = [get_color_byte(name) for name in ('red', 'green', 'blue')]
rgb = (r << 16) | (g << 8 ) | b
print '%06X' % rgb
test
Enter a value from 0 to 255 for red: hello
invalid literal for int() with base 10: 'hello'
Enter a value from 0 to 255 for red: 12.5
invalid literal for int() with base 10: '12.5'
Enter a value from 0 to 255 for red: -5
Out of range
Enter a value from 0 to 255 for red: 300
Out of range
Enter a value from 0 to 255 for red: 240
Enter a value from 0 to 255 for green: 13
Enter a value from 0 to 255 for blue: 7
F00D07
As an alternative to print '%06X' % rgb
you could use the more modern format
function:
print format(rgb, '06X')
In both of those, the capital X
says to use capital letters in the hex string.
Upvotes: 0
Reputation: 1129
You can check the condition on the three colors calling separately the function and then compare all them in the same statement:
red_ok = check_rgb(red)
green_ok = check_rgb(green)
blue_ok = check_rgb(blue)
if (red_ok and green_ok and blue_ok) :
val = (red << 16) + (green << 8) + blue
print '%s' % (hex(val)[2:]).upper()
Upvotes: 0
Reputation: 1787
You need to do something like something like
red = check_rgb(red)
When you just do
check_rgb(red)
The return value is not used.
Upvotes: 2