Using the OR function

Thread Solved

Join Date: Sep 2009
Posts: 18
Reputation: chico2009 is an unknown quantity at this point 
Solved Threads: 0
chico2009 chico2009 is offline Offline
Newbie Poster

Using the OR function

 
0
  #1
Sep 21st, 2009
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

  1. # Convert C to F or F to C and return with result
  2.  
  3. select = True
  4. while select:
  5. x = int(raw_input ('To convert Celsius to Farenheit, enter 1\nTo convert Farenheit to Celsius, enter 2\n'))
  6. if x == 1 or x == 2:
  7. select = False
  8. else:
  9. print 'Only options are 1 or 2'
  10.  
  11. #Convert C to F
  12. if x == 1:
  13. def Tc_to_Tf ():
  14. Tc = input ("What is the Celsius Temperature ? " )
  15. Tf = 9.0/5.0 * Tc + 32
  16. return Tf
  17. Tf = Tc_to_Tf ()
  18. #.2f instructs Python that placeholder holds float number with 2decimal places.
  19. print 'The temperature is %.2f Degrees Fahrenheit' %(Tf, )
  20.  
  21. #Convert F to C
  22. if x == 2:
  23. def Tf_to_Tc ():
  24. Tf = input ("What is the Fahrenheit Temperature ? " )
  25. Tc = (5.0/9.0)*(Tf - 32)
  26. return Tc
  27. Tc = Tf_to_Tc ()
  28. 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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 354
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 44
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Whiz

Re: Using the OR function

 
0
  #2
Sep 21st, 2009
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
Last edited by gerard4143; Sep 21st, 2009 at 1:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 149
Reputation: jice is on a distinguished road 
Solved Threads: 38
jice jice is offline Offline
Junior Poster

Re: Using the OR function

 
0
  #3
Sep 21st, 2009
I would do
  1. if x in [1,2]:
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 306
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 52
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: Using the OR function

 
1
  #4
Sep 21st, 2009
Originally Posted by chico2009 View Post
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

  1. # Convert C to F or F to C and return with result
  2.  
  3. select = True
  4. while select:
  5. x = int(raw_input ('To convert Celsius to Farenheit, enter 1\nTo convert Farenheit to Celsius, enter 2\n'))
  6. if x == 1 or x == 2:
  7. select = False
  8. else:
  9. print 'Only options are 1 or 2'
  10.  
  11. #Convert C to F
  12. if x == 1:
  13. def Tc_to_Tf ():
  14. Tc = input ("What is the Celsius Temperature ? " )
  15. Tf = 9.0/5.0 * Tc + 32
  16. return Tf
  17. Tf = Tc_to_Tf ()
  18. #.2f instructs Python that placeholder holds float number with 2decimal places.
  19. print 'The temperature is %.2f Degrees Fahrenheit' %(Tf, )
  20.  
  21. #Convert F to C
  22. if x == 2:
  23. def Tf_to_Tc ():
  24. Tf = input ("What is the Fahrenheit Temperature ? " )
  25. Tc = (5.0/9.0)*(Tf - 32)
  26. return Tc
  27. Tc = Tf_to_Tc ()
  28. 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:
  1. 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:
  1. if 0:
  2. print "0 is True"
  3. else:
  4. print "0 is False"
  5.  
  6. if -1:
  7. print "-1 is True"
  8. else:
  9. print "-1 is False"

Here's a final example:
  1. sentinel=True # a boolean variable...we'll use this in a bit!
  2. value = 24 #and a number
  3.  
  4. # OK, we want to check that value is greater than -1
  5. # and less than or equal to 23...
  6. if value>-1 and 23:# oops, the right hand part of this always evaluates to true
  7. print "1. True" # this gets printed...Doh!
  8. else:
  9. print "1. False" # when we want this!
  10.  
  11. if -1 and value<=23: # Oops, now the left hand part always evaluates to True
  12. print "2. True"
  13. else:
  14. print "2. False" # returns false, but it's still wrong! ^^
  15.  
  16. # What we meant was..
  17. if value>-1 and value<=23: #phew now we're ok. Both sides are valid boolean expresions
  18. print "3. True..."
  19. else:
  20. print "3. False..." # should return false!
  21.  
  22. # Something slightly different:
  23. # Here we're using the value of a variable on one side, but it's safe
  24. # to do so because it's a boolean variable!
  25. # if sentinel is true and value is greater than 23 return true...
  26. if sentinel and value>23: # both sides of the 'or' are boolean
  27. print "4. True...." # Yaay!
  28. else:
  29. print "4. False..."

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

Cheers for now,
Jas.
Last edited by JasonHippy; Sep 21st, 2009 at 3:01 pm. Reason: edits
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster

Re: Using the OR function

 
0
  #5
Sep 21st, 2009
Defining and then calling the functions under the if() statements serves no purpose. Stripped to the minimum, it would be:
  1. #Convert C to F
  2. if x == 1:
  3. Tc = input ("What is the Celsius Temperature ? " )
  4. print 'The temperature is %.2f Degrees Fahrenheit' % \
  5. (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.
  1. def Tc_to_Tf ():
  2. Tc = input ("What is the Celsius Temperature ? " )
  3. print 'The temperature is %.2f Degrees Fahrenheit' %(9.0/5.0 * Tc + 32 )
  4.  
  5. def Tf_to_Tc ():
  6. Tf = input ("What is the Fahrenheit Temperature ? " )
  7. print 'The temperature is %.2f Degrees Celsius' %((5.0/9.0)*(Tf - 32) )
  8.  
  9. ##--------------------------------------------------------------------------------------------------
  10. ## "select" is a python function and should not be used as a variable
  11. selection = True
  12. while selection:
  13. x = int(raw_input ('To convert Celsius to Farenheit, enter 1\nTo convert Farenheit to Celsius, enter 2\n'))
  14. if x == 1:
  15. Tc_to_Tf ()
  16. selection = False
  17. elif x == 2:
  18. Tf_to_Tc ()
  19. selection = False
  20. else:
  21. 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.
Last edited by woooee; Sep 21st, 2009 at 4:00 pm.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 46
snippsat snippsat is offline Offline
Junior Poster

Re: Using the OR function

 
0
  #6
Sep 21st, 2009
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.

  1. ##--------------------------------------------------------------------------------------------------
  2. ## "select" is a python function and should not be used as a variable
  3. selection = True
  4. while selection:
  5. try:
  6. x = int(raw_input ('To convert Celsius to Farenheit, enter 1\nTo convert Farenheit to Celsius, enter 2\n'))
  7. if x == 1:
  8. Tc_to_Tf ()
  9. selection = False
  10. elif x == 2:
  11. Tf_to_Tc ()
  12. selection = False
  13. else:
  14. print 'Only options are 1 or 2'
  15.  
  16. except:
  17. print 'No letter please'
Last edited by snippsat; Sep 21st, 2009 at 5:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Using the OR function

 
1
  #7
Sep 21st, 2009
I made an attempt on user friendly data entry with just such a simple temperature converter program, see:
http://www.daniweb.com/forums/showth...595#post992595
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster

Re: Using the OR function

 
0
  #8
Sep 21st, 2009
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.
  1. def f2c(t):
  2. """given t Fahrenheit return Celsius"""
  3. return 5/9.0 * (t - 32)
  4.  
  5. def c2f(t):
  6. """given t Celsius return Fahrenheit"""
  7. return 9/5.0 * t + 32
Last edited by woooee; Sep 21st, 2009 at 7:57 pm.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,003
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Using the OR function

 
0
  #9
Sep 21st, 2009
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.
Last edited by vegaseat; Sep 21st, 2009 at 11:09 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: chico2009 is an unknown quantity at this point 
Solved Threads: 0
chico2009 chico2009 is offline Offline
Newbie Poster

Re: Using the OR function

 
0
  #10
Sep 23rd, 2009
Hi Folks
Thanks to all of you for your help, it is much appreciated
Graham
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC