List Info

Thread: celcius to farenheit converter.




celcius to farenheit converter.
country flaguser name
United States
2007-04-17 23:34:01
I found this site and I'm practicing coding and I write this
script, but
I'm unsure why its not working.  Everything goes well until
it gets to the
part where it tries to calculate the formula.  Inputs work
fine anyone
know what I did wrong?

###################################
#Temperature Converter
#Coding Practice for lamonte(uni-code.com)
###################################

temp = raw_input("Insert a temperature to
convert.n")

type = raw_input("Now choose a convertion: Celcius(c)
or Farenheit(f)")

if type == "c":
	cel = (5/9)*(temp-32)
	print "Farhrenheit" +temp+" is equal to
"+cel+" celcius.n"
elif type == "f":
	far = (9/5)*(temp+32)
	print "Farhrenheit" +far+" is equal to
"+temp+" celcius.n"
else:
	print "Unknown Syntax!n";

raw_input("nPress enter to close program")
_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

Re: celcius to farenheit converter.
user name
2007-04-17 23:49:22
On 4/18/07, pythonuni-code.com <pythonuni-code.com> wrote:

> I found this site and I'm practicing coding and I write
this script, but
> I'm unsure why its not working.  Everything goes well
until it gets to the
> part where it tries to calculate the formula.  Inputs
work fine anyone
> know what I did wrong?

In the future, please add what error message you get. That
saves us
some type cutting and pasting the code, and running it.

The thing that's wrong with your code is that you try to
multiply an
integer with a string. raw_input() returns a string, not an
integer.
What you want to do is to convert it to an int first:

temp = raw_input("Insert a temperature to
convert.n")
temp_int = int(temp)

Also, print cannot mix ints and strings using string
concatenation
(+). What you want to do is to use string formats.

So, the final code would be something like this:

temp = raw_input("Insert a temperature to
convert.n")
temp_int = int(temp)

type = raw_input("Now choose a convertion: Celcius(c)
or Farenheit(f)")

if type == "c":
       cel = (5/9)*(temp_int-32)
       print "Farhrenheit %d is equal to %d
celcius.n" % (temp_int, cel)
elif type == "f":
       far = (9/5)*(temp+32)
       print "Farhrenheit %d is equal to %d
celcius.n" % (temp_int, far)
else:
       print "Unknown Syntax!n";

raw_input("nPress enter to close program")



-- 
- Rikard - http://bos.hack.org/cv/
_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

Re: celcius to farenheit converter.
country flaguser name
United States
2007-04-18 00:07:39
Rikard Bosnjakovic wrote:
> On 4/18/07, pythonuni-code.com <pythonuni-code.com> wrote:
>
>   
>> I found this site and I'm practicing coding and I
write this script, but
>> I'm unsure why its not working.  Everything goes
well until it gets to the
>> part where it tries to calculate the formula. 
Inputs work fine anyone
>> know what I did wrong?
>>     
>
> if type == "c":
>        cel = (5/9)*(temp_int-32)
>   
a side-note:
5/9 is 0 in Python 2.5 and earlier.
'/' defaults to integer division if both sides are integers
as well.
What you'd want to do is 5.0/9  or 5/9.0 or 5.0/9.0 or just
0.555555555
same applies to your 9/5 below.
>        print "Farhrenheit %d is equal to %d
celcius.n" % (temp_int, cel)
> elif type == "f":
>        far = (9/5)*(temp+32)

_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )