Help a Novice with function please

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
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

Help a Novice with function please

 
0
  #1
Sep 18th, 2009
Hi All
Could you please help me with the following,
[code]
#Convert C to F
def Tc_to_Tf ():
Tc = input ("What is the Celsius Temperature ? " )
Tf = 9.0/5.0 * Tc + 32
print 'The temperature is ', Tf, ' Degrees Fahrenheit'
[code]

I get NameError: name 'Tf' is not defined
I dont understand as Tf is defined as a formulae?
Thanks in anticipation
Graham
Ps, dont think I have these tags right, some help here too please
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 376
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 47
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Whiz

Re: Help a Novice with function please

 
0
  #2
Sep 18th, 2009
This works on my machine

#! /usr/bin/python

#Convert C to F
def Tc_to_Tf():
	Tc = input ("What is the Celsius Temperature ? " )
	Tf = 9.0/5.0 * Tc + 32
	print 'The temperature is ', Tf, ' Degrees Fahrenheit'

Tc_to_Tf()
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,284
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 177
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Help a Novice with function please

 
0
  #3
Sep 18th, 2009
Like gerard4143 says, you have to make the print statement part of the function, since Tf is local to the function. The other option would be to return Tf to make it available outside the function:
  1. #Convert C to F
  2. def Tc_to_Tf ():
  3. Tc = input ("What is the Celsius Temperature ? " )
  4. Tf = 9.0/5.0 * Tc + 32
  5. return Tf
  6.  
  7. Tf = Tc_to_Tf ()
  8. print 'The temperature is ', Tf, ' Degrees Fahrenheit'
Please use the [code=python] and [/code] tag pair to enclose your python code.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,401
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Help a Novice with function please

 
0
  #4
Sep 19th, 2009
Alternatively you can use placeholder (same Snee's example)
  1.  
  2. #Convert C to F
  3. def Tc_to_Tf ():
  4. Tc = input ("What is the Celsius Temperature ? " )
  5. Tf = 9.0/5.0 * Tc + 32
  6. return Tf
  7.  
  8. Tf = Tc_to_Tf ()
  9. #.2f instructs Python that placeholder holds float number with 2decimal places. try 3f, 4f etc and see how it does
  10. print 'The temperature is %.2f Degrees Fahrenheit' %(Tf, )
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
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: Help a Novice with function please

 
0
  #5
Sep 19th, 2009
Hi Guys
Thanks very much for your post.
I understand now that I have to make Tf part of the function and how to do so.
I dont understand your Tf=Tc_toTf () command, could you please explain how it works.

Many thanks
Graham
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,070
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: 938
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Help a Novice with function please

 
0
  #6
Sep 19th, 2009
Originally Posted by chico2009 View Post
Hi Guys
Thanks very much for your post.
I understand now that I have to make Tf part of the function and how to do so.
I dont understand your Tf=Tc_toTf () command, could you please explain how it works.

Many thanks
Graham
It simply means that function Tc_toTf() returns a value that you want to put into variable TF.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 167
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 49
snippsat's Avatar
snippsat snippsat is offline Offline
Junior Poster

Re: Help a Novice with function please

 
0
  #7
Sep 19th, 2009
  1. def Tc_to_Tf ():
  2. Tc = input ("What is the Celsius Temperature ? " )
  3. Tf = 9.0/5.0 * Tc + 32
  4. return Tf
Run it an nothing happends it only return Tf
Let do something simple in idle.

  1. IDLE 2.6.2
  2. >>> Tf = 50
  3. >>> print 'The temperature is %.2f Degrees Fahrenheit' % (Tf)
  4. The temperature is 50.00 Degrees Fahrenheit
  5. >>>
So the only diffrence is that we put function call i a variable.
Tf = Tc_to_Tf ()
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: Help a Novice with function please

 
0
  #8
Sep 20th, 2009
Hi Folks
Thanks all for your help
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC