473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Need help with celcius to fahrenheit converter code

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
Jan 22 '08 #1
1 5884
rhitam30111985
112 100+
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.  
Jan 23 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Tommy Lang | last post by:
I am trying to learn to use dynamic variables. I have pasted the code below. Is this the proper way of using dynamic variables? Thanks, Tommy ...
4
by: Paul M | last post by:
Larry, i've got an xml file which has multiple cities as the top level item. When i read it in using XMLReader, and define the variables, as below, it finds the correct number of cities in the...
1
by: sjaak | last post by:
Hi, Im just starting out with C#, I try to make a console application where the user has to fill in temperature in celsius degrees and gets fahrenheit, my code sucks ; can anyone help me out with...
5
by: bratiskovci | last post by:
Hello, I am trying to write a few programs using python but don't really know where to start...I am completely CONFUSED. My first program deals with conversion between fahrenheit and celcius...
6
by: bratiskovci | last post by:
1. How do I change the program so that the program does not terminate after completing one conversion. Instead, the program should continue to convert values until the user indicates that he/she...
7
by: 05l8kr | last post by:
I'm suppose to write a program to convert a temperature from Fahrenheit to Celsius or from Celsius to Fahrenheit. * Ask the user which way they want to convert (use a menu 1 = Fahrenheit to...
1
by: MichiganMan | last post by:
I have to do a simple temp conversion page in Javascript. I wrote code that I thought was right, but it is not showing output after I click the calculate button. Could someone PLEASE tell me what I...
2
by: lifeshortlivitup | last post by:
I had to create a program that allows the user to input a temp and then click on either the convert to fahrenheit or convert to celsius button and then display that result within the textbox that...
2
by: silhutte75 | last post by:
Writing program to convert celcius to fahrenheit and vice versa. Have eliminated all errors but have four warnings and and stuck. Please help help help. snippet of code: printf("2. Please...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.