Hi folks
I have just completed my first programme with help from yourselves.
I cant understand why the code , if x ==1 or 2, wouldn't work in my programme below

# Convert C to F or F to C and return with result

select = True
while select:
    x = int(raw_input ('To convert Celsius to Farenheit, enter 1\nTo convert Farenheit to Celsius, enter 2\n'))   
    if x == 1 or x == 2:
        select = False
    else:
        print 'Only options are 1 or 2'            

#Convert C to F
if x == 1:
    def Tc_to_Tf ():        
        Tc = input ("What is the Celsius Temperature ?   " )
        Tf = 9.0/5.0 * Tc + 32
        return Tf
    Tf = Tc_to_Tf ()
    #.2f instructs Python that placeholder holds float number with 2decimal places. 
    print 'The temperature is %.2f Degrees Fahrenheit' %(Tf, )
        
#Convert F to C
if x == 2:
    def Tf_to_Tc ():
        Tf = input ("What is the Fahrenheit Temperature ?   " )
        Tc = (5.0/9.0)*(Tf - 32)
        return Tc
    Tc = Tf_to_Tc ()
    print 'The temperature is %.2f Degrees Celsius' %(Tc, )

Many thanks in anticipation. I included the whole programme as I would value any comments on my methods.
Graham

try x ==1 or x == 2 your version is always true x ==1 or 2 since 2 is not 0 so anything or'd with a true value is true

I would do

if x in [1,2]:

Hi folks
I have just completed my first programme with help from yourselves.
I cant understand why the code , if x ==1 or 2, wouldn't work in my programme below

# Convert C to F or F to C and return with result

select = True
while select:
    x = int(raw_input ('To convert Celsius to Farenheit, enter 1\nTo convert Farenheit to Celsius, enter 2\n'))   
    if x == 1 or x == 2:
        select = False
    else:
        print 'Only options are 1 or 2'            

#Convert C to F
if x == 1:
    def Tc_to_Tf ():        
        Tc = input ("What is the Celsius Temperature ?   " )
        Tf = 9.0/5.0 * Tc + 32
        return Tf
    Tf = Tc_to_Tf ()
    #.2f instructs Python that placeholder holds float number with 2decimal places. 
    print 'The temperature is %.2f Degrees Fahrenheit' %(Tf, )
        
#Convert F to C
if x == 2:
    def Tf_to_Tc ():
        Tf = input ("What is the Fahrenheit Temperature ?   " )
        Tc = (5.0/9.0)*(Tf - 32)
        return Tc
    Tc = Tf_to_Tc ()
    print 'The temperature is %.2f Degrees Celsius' %(Tc, )

Many thanks in anticipation. I included the whole programme as I would value any comments on my methods.
Graham

I can't see a problem with the code you've posted!

However if you were using this before:

if x==1 or 2:

Then as gerard has said, the if statement would always evaluate to True...

for example if the user entered '4' as their first choice, the if statement would be evaluated like this:
Evaluating the left hand side of the or statement...
x is not equal to 1, so x==1 evaluates to False (4 is definitely not equal to 1!).

So to the interpreter; after the first part of the evaluation, your if statement looks kinda like this:
"if False or 2:"

Now it evaluates the right hand side of the or statement...
Because '2' is simply a value and not a boolean expression which evaluates to True or False or a boolean variable, this side will always evaluate to True.

What's going on here is at this point the interpreter will see "if False or 2" and will think "2?? Yes, 2 is definitely 2 so for the right hand side of the expression I'll return True!"
so your if statement now looks like this:
"if False or True:"

Now, if you paid any attention in class, you'd know that 'OR' statements (and logical OR gates in electonics) evaluate to/output True if one or more of the sides of the expression (or one or more of the inputs in electronics) evaluates to True.

Therefore the statement "if x==1 or 2:" will always evaluate to True because of the right hand side of your original expression...hence your original problem.

And it doesn't matter what values you put into any of the boolean operations (if, while, or, and, not etc..).
If any side of any of the boolean operations is not a boolean expression or a boolean variable, then it will always evaluate to true...

The only exception is if the value is 0, then the interpreter returns False...Negative values also evaluate to True!

try this:

if 0:
    print "0 is True"
else:
    print "0 is False"
    
if -1:
    print "-1 is True"
else:
    print "-1 is False"

Here's a final example:

sentinel=True  # a boolean variable...we'll use this in a bit!
value = 24 #and a number

# OK, we want to check that value is greater than -1
# and less than or equal to 23...
if value>-1 and 23:# oops, the right hand part of this always evaluates to true 
    print "1. True" # this gets printed...Doh!
else:
    print "1. False" # when we want this!

if -1 and value<=23: # Oops, now the left hand part always evaluates to True
    print "2. True"
else:
    print "2. False" # returns false, but it's still wrong! ^^

# What we meant was..
if value>-1 and value<=23: #phew now we're ok. Both sides are valid boolean expresions
    print "3. True..."
else:
    print "3. False..." # should return false!

# Something slightly different:
# Here we're using the value of a variable on one side, but it's safe
# to do so because it's a boolean variable!
# if sentinel is true and value is greater than 23 return true...
if sentinel and value>23: # both sides of the 'or' are boolean
    print "4. True...." # Yaay!
else:
    print "4. False..."

Hope that clears things up for you once and for all!

Cheers for now,
Jas.

commented: great explanation +8

Defining and then calling the functions under the if() statements serves no purpose. Stripped to the minimum, it would be:

#Convert C to F
if x == 1:
    Tc = input ("What is the Celsius Temperature ?   " )
    print 'The temperature is %.2f Degrees Fahrenheit' % \
             (9.0/5.0 * Tc + 32 )

And generally, it would be more along the lines of this to avoid a double compare on "x", although there is nothing wrong with the way you did it.

def Tc_to_Tf ():        
    Tc = input ("What is the Celsius Temperature ?   " )
    print 'The temperature is %.2f Degrees Fahrenheit' %(9.0/5.0 * Tc + 32 )

def Tf_to_Tc ():
    Tf = input ("What is the Fahrenheit Temperature ?   " )
    print 'The temperature is %.2f Degrees Celsius' %((5.0/9.0)*(Tf - 32) )

##--------------------------------------------------------------------------------------------------
## "select" is a python function and should not be used as a variable
selection = True
while selection:
    x = int(raw_input ('To convert Celsius to Farenheit, enter 1\nTo convert Farenheit to Celsius, enter 2\n'))   
    if x == 1:
        Tc_to_Tf ()
        selection = False
    elif x == 2:
        Tf_to_Tc ()
        selection = False
    else:
        print 'Only options are 1 or 2'

Finally, check your arithmetic.
This subtracts 32 from Tf and then multiplies
(5.0/9.0)*(Tf - 32)

This multiplies Tc by 9/5 and then adds 32
9.0/5.0 * Tc + 32

When in doubt, use parens.

Just a littel change from the good suggestion from woooee.
Type a letter:@
Never trust the user,there are always someone that dont follow instruction well.
If you want to look into this it`s called Exceptions handling
The other choice is to write the menu so that you dont need exceptions handling.

##--------------------------------------------------------------------------------------------------
## "select" is a python function and should not be used as a variable
selection = True
while selection:
    try:
        x = int(raw_input ('To convert Celsius to Farenheit, enter 1\nTo convert Farenheit to Celsius, enter 2\n'))   
        if x == 1:
            Tc_to_Tf ()
            selection = False
        elif x == 2:
            Tf_to_Tc ()
            selection = False
        else:
            print 'Only options are 1 or 2'
            
    except:
        print 'No letter please'

Note that you have a similar problem with the arithmetic. In the first function you subtract 32 from t and multiply by 5/9. In c2f you multiply t by 9/5 and then add 32. Perhaps this is the correct way to do it and I'm wrong, in which case appologies to the OP. I just use 9C + 160 = 5F for both conversions and solve for the unknown variable.

def f2c(t):
    """given t Fahrenheit return Celsius"""
    return 5/9.0 * (t - 32)
 
def c2f(t):
    """given t Celsius return Fahrenheit"""
    return 9/5.0 * t + 32

I always test for -40, it should be the same for both. Snee's approach seems to work correctly.

Or even simpler, 0 C gives 32 F, and 32 F should give 0 C. Something you can do by just looking at the two formulas.

Hi Folks
Thanks to all of you for your help, it is much appreciated
Graham

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.