Connecting Tech Pros Worldwide Forums | Help | Site Map

Need help with celcius to fahrenheit converter code

Newbie
 
Join Date: Jan 2008
Posts: 15
#1: Jan 22 '08
I'm new to Python and am trying to figure out how to streamline the code at bottom to get it more simple but am having trouble. I was hoping that someone here could help me out?

The program I am trying to create simply asks the terminal operator for five different celsius temperatures, processes them into fahrenheit values and outputs them to the screen. The results should look something like this:

Expand|Select|Wrap|Line Numbers
  1. This is a program that converts celsius to Fahrenheit
  2. Enter 5 Celsius temperatures seperated by a comma: 1,2,3,4,5
  3. ------------------------------------------------------------
  4. 1 degrees Celsius is 33.8 degrees Fahrenheit.
  5. 2 degrees Celsius is 35.6 degrees Fahrenheit.
  6. 3 degrees Celsius is 37.4 degrees Fahrenheit.
  7. 4 degrees Celsius is 39.2 degrees Fahrenheit.
  8. 5 degrees Celsius is 41.0 degrees Fahrenheit.
Here is my code now, it works but as you can see it is bulky and repetative. Theres got to be a simple way to streamline it:
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     print "\nThis is a program that converts five celsius values to Fahrenheit"
  3.     ctemp1, ctemp2, ctemp3, ctemp4, ctemp5 = input("\nEnter 5 Celsius temperatures seperated by a comma: ")
  4.     ftemp1 = 9.0 / 5.0 * ctemp1 + 32
  5.     ftemp2 = 9.0 / 5.0 * ctemp2 + 32
  6.     ftemp3 = 9.0 / 5.0 * ctemp3 + 32
  7.     ftemp4 = 9.0 / 5.0 * ctemp4 + 32
  8.     ftemp5 = 9.0 / 5.0 * ctemp5 + 32
  9.     print "------------------------------------------------------------"
  10.     print ctemp1, "degrees Celsius is", ftemp1, "degrees Fahrenheit."
  11.     print ctemp2, "degrees Celsius is", ftemp2, "degrees Fahrenheit."
  12.     print ctemp3, "degrees Celsius is", ftemp3, "degrees Fahrenheit."
  13.     print ctemp4, "degrees Celsius is", ftemp4, "degrees Fahrenheit."
  14.     print ctemp5, "degrees Celsius is", ftemp5, "degrees Fahrenheit."
  15.  
  16. main()
Help? Thanks
rhitam30111985's Avatar
Member
 
Join Date: Aug 2007
Location: bangalore, india
Posts: 111
#2: Jan 23 '08

re: Need help with celcius to fahrenheit converter code


Quote:

Originally Posted by sigkill9

I'm new to Python and am trying to figure out how to streamline the code at bottom to get it more simple but am having trouble. I was hoping that someone here could help me out?

The program I am trying to create simply asks the terminal operator for five different celsius temperatures, processes them into fahrenheit values and outputs them to the screen. The results should look something like this:

Expand|Select|Wrap|Line Numbers
  1. This is a program that converts celsius to Fahrenheit
  2. Enter 5 Celsius temperatures seperated by a comma: 1,2,3,4,5
  3. ------------------------------------------------------------
  4. 1 degrees Celsius is 33.8 degrees Fahrenheit.
  5. 2 degrees Celsius is 35.6 degrees Fahrenheit.
  6. 3 degrees Celsius is 37.4 degrees Fahrenheit.
  7. 4 degrees Celsius is 39.2 degrees Fahrenheit.
  8. 5 degrees Celsius is 41.0 degrees Fahrenheit.
Here is my code now, it works but as you can see it is bulky and repetative. Theres got to be a simple way to streamline it:
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     print "\nThis is a program that converts five celsius values to Fahrenheit"
  3.     ctemp1, ctemp2, ctemp3, ctemp4, ctemp5 = input("\nEnter 5 Celsius temperatures seperated by a comma: ")
  4.     ftemp1 = 9.0 / 5.0 * ctemp1 + 32
  5.     ftemp2 = 9.0 / 5.0 * ctemp2 + 32
  6.     ftemp3 = 9.0 / 5.0 * ctemp3 + 32
  7.     ftemp4 = 9.0 / 5.0 * ctemp4 + 32
  8.     ftemp5 = 9.0 / 5.0 * ctemp5 + 32
  9.     print "------------------------------------------------------------"
  10.     print ctemp1, "degrees Celsius is", ftemp1, "degrees Fahrenheit."
  11.     print ctemp2, "degrees Celsius is", ftemp2, "degrees Fahrenheit."
  12.     print ctemp3, "degrees Celsius is", ftemp3, "degrees Fahrenheit."
  13.     print ctemp4, "degrees Celsius is", ftemp4, "degrees Fahrenheit."
  14.     print ctemp5, "degrees Celsius is", ftemp5, "degrees Fahrenheit."
  15.  
  16. main()
Help? Thanks


u can use the for loop :


Expand|Select|Wrap|Line Numbers
  1.  
  2. for i in range(1,5):
  3.    print i  , " degree celcius is " , i * 9/5.0 + 32 , "degree farenheit " 
  4.  
  5.  
Reply