Function to check a variable and change it
<p>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:</p>

<pre><code>invalid_msg = 'Whoops looks like you have entered incorrect information'
def check_rgb(var):
    while var &gt; 255 or var &lt; 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 &gt; 255 or red &lt; 0:
        print invalid_msg
        green = int(raw_input('Enter your value for green.'))
    blue = int(raw_input('Enter your value for blue.'))
    while blue &gt; 255 or red &lt; 0:
        print invalid_msg
        blue = int(raw_input('Enter your value for blue.'))
    val = (red &lt;&lt; 16) + (green &lt;&lt; 8) + blue
    print '%s' % (hex(val)[2:]).upper()

rgb_hex()
</code></pre>

<p>The issue is with redeclaring the variable. Right now this stores the value entered into the function to the variable 'var' not red.</p>

<p>As Davis Yoshida mentions, merely doing <code>check_rgb(red)</code> doesn't change the original value of <code>red</code>, since you forgot to save the value that <code>check_rgb</code> returns.</p>

<p>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.  </p>

<pre><code>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 &lt;= v &lt;= 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 &lt;&lt; 16) | (g &lt;&lt; 8 ) | b
print '%06X' % rgb
</code></pre>

<p><strong>test</strong></p>

<pre class="lang-none prettyprint-override"><code>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
</code></pre>

<p>As an alternative to <code>print '%06X' % rgb</code> you could use the more modern <code>format</code> function:</p>

<pre><code>print format(rgb, '06X')
</code></pre>

<p>In both of those, the capital <code>X</code> says to use capital letters in the hex string.</p>

python python-2.7