| | |
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, )
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
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
•
•
•
•
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 |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog decimals dictionaries dictionary directory drive dynamic error examples excel exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyqt python random recursion schedule scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib






