<div dir="ltr"><br><br><div class="gmail_quote">On Thu, Aug 14, 2008 at 7:08 PM, Joseph Bae <span dir="ltr"><<a href="mailto:joeturf@gmail.com">joeturf@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div dir="ltr">Hi all,<br><br>I'm new to Python (and programming in general) and need some help!<br><br>Here is my code so far for a temperature conversion program (converts between Fahrenheit and Celsius):<br><br><span style="color: rgb(51, 51, 255);"><br>
<br>temp = input("Enter A Number : ")</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">convertTo = raw_input("Convert To (F)ahrenheit or (C)elsius? : ")</span><br><br>
<span style="color: rgb(51, 51, 255);">if convertTo == "F":</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"> convertedTemp = convertToFahrenheit(temp)</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> print "%d Celsius = %d Fahrenheit" % (temp, convertedTemp)</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);">else:</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> convertedTemp = convertToCelsius(temp)</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> print "%d Fahrenheit = %d Celsius" % (temp, convertedTemp)</span><br style="color: rgb(51, 51, 255);">
<br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">def convertToFahrenheit(t):</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> tF = (9.0/5.0) * (t + 32)</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"> return tF</span><br style="color: rgb(51, 51, 255);"><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">def convertToCelsius(t):</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"> tC = (9.0/5.0) * (t - 32)</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> return tC</span><br><br><br><br>It worked fine without having extra functions but once I put convertToFahrenheit and convertToCelsius in (just for practice really), this happened:<br>
<br>Enter A Number : 50<br>Convert to (F)ahrenheit or (C)elsius? : F<br><span style="color: rgb(255, 0, 0);">Traceback (most recent call last):</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);"> File "TemperatureConverter.py", line 5, in <module></span><br style="color: rgb(255, 0, 0);">
<span style="color: rgb(255, 0, 0);"> convertedTemp = convertToFahrenheit(temp)</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">NameError: name 'convertToFahrenheit' is not defined</span><br>
<br>This is most likely a very simple error, but can someone please clarify for me why it's behaving this way?</div></blockquote><div><br>You just need to define the functions before you use them<br>Arranging the order od your program makes it work. Like this<br>
def convertToFahrenheit(t):<br> tF = (9.0/5.0) * (t + 32)<br> return tF<br><br>def convertToCelsius(t):<br> tC = (9.0/5.0) * (t - 32)<br> return tC<br>temp = input("Enter A Number : ")<br>convertTo = raw_input("Convert To (F)ahrenheit or (C)elsius? : ")<br>
<br>if convertTo == "F":<br> convertedTemp = convertToFahrenheit(temp)<br> print "%d Celsius = %d Fahrenheit" % (temp, convertedTemp)<br>else:<br> convertedTemp = convertToCelsius(temp)<br> print "%d Fahrenheit = %d Celsius" % (temp, convertedTemp)<br>
<br> <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div dir="ltr"><br><br>Thanks!<br><br>Joe<br></div>
<br>_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br>لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد الغزالي<br>"No victim has ever been more repressed and alienated than the truth"<br>
<br>Emad Soliman Nawfal<br>Indiana University, Bloomington<br><a href="http://emnawfal.googlepages.com">http://emnawfal.googlepages.com</a><br>--------------------------------------------------------<br>
</div>