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>You need to do something like something like</p>

<pre><code>red = check_rgb(red)
</code></pre>

<p>When you just do</p>

<pre><code>check_rgb(red)
</code></pre>

<p>The return value is not used.</p>

python python-2.7