| | |
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:
Solved Threads: 0
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
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
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() 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:
Please use the [code=python] and [/code] tag pair to enclose your python code.
python Syntax (Toggle Plain Text)
#Convert C to F def Tc_to_Tf (): Tc = input ("What is the Celsius Temperature ? " ) Tf = 9.0/5.0 * Tc + 32 return Tf Tf = Tc_to_Tf () print 'The temperature is ', Tf, ' Degrees Fahrenheit'
No one died when Clinton lied.
Alternatively you can use placeholder (same Snee's example)
python Syntax (Toggle Plain Text)
#Convert C to F 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. try 3f, 4f etc and see how it does print 'The temperature is %.2f Degrees Fahrenheit' %(Tf, )
•
•
•
•
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
May 'the Google' be with you!
python Syntax (Toggle Plain Text)
def Tc_to_Tf (): Tc = input ("What is the Celsius Temperature ? " ) Tf = 9.0/5.0 * Tc + 32 return Tf
Let do something simple in idle.
python Syntax (Toggle Plain Text)
IDLE 2.6.2 >>> Tf = 50 >>> print 'The temperature is %.2f Degrees Fahrenheit' % (Tf) The temperature is 50.00 Degrees Fahrenheit >>>
Tf = Tc_to_Tf () ![]() |
Similar Threads
- My python program/function! (Python)
- Recursive Function Problem (C++)
- Need help with mock substr function and output (C)
- function declare in main() and outside main() (C)
- Pow Function (C++)
- print function not working (C++)
Other Threads in the Python Forum
- Previous Thread: Indexing two lists
- Next Thread: Why can't i install cTurtle on my Ubuntu version of python?
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied apache application argv beginner book change code color dictionary dynamic edit editing enter examples excel file filename float format ftp function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path phonebook port prime print program programming projects py2exe pygame pyopengl pyqt python random recursion recursive redirect remote reverse scrolledtext server session simple smtp software sprite ssh statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






