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:
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.