943,542 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 730
  • Python RSS
Sep 18th, 2009
0

Help a Novice with function please

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chico2009 is offline Offline
18 posts
since Sep 2009
Sep 18th, 2009
0

Re: Help a Novice with function please

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()
Reputation Points: 499
Solved Threads: 366
Postaholic
gerard4143 is online now Online
2,195 posts
since Jan 2008
Sep 18th, 2009
0

Re: Help a Novice with function please

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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Sep 19th, 2009
0

Re: Help a Novice with function please

Alternatively you can use placeholder (same Snee's example)
python Syntax (Toggle Plain Text)
  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, )
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Sep 19th, 2009
0

Re: Help a Novice with function please

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chico2009 is offline Offline
18 posts
since Sep 2009
Sep 19th, 2009
0

Re: Help a Novice with function please

Click to Expand / Collapse  Quote originally posted by chico2009 ...
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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Sep 19th, 2009
0

Re: Help a Novice with function please

python Syntax (Toggle Plain Text)
  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.

python Syntax (Toggle Plain Text)
  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 ()
Reputation Points: 280
Solved Threads: 278
Master Poster
snippsat is offline Offline
770 posts
since Aug 2008
Sep 20th, 2009
0

Re: Help a Novice with function please

Hi Folks
Thanks all for your help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chico2009 is offline Offline
18 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Indexing two lists
Next Thread in Python Forum Timeline: Why can't i install cTurtle on my Ubuntu version of python?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC