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:

This is a program that converts celsius to Fahrenheit
Enter 5 Celsius temperatures seperated by a comma: 1,2,3,4,5
------------------------------------------------------------
1 degrees Celsius is 33.8 degrees Fahrenheit.
2 degrees Celsius is 35.6 degrees Fahrenheit.
3 degrees Celsius is 37.4 degrees Fahrenheit.
4 degrees Celsius is 39.2 degrees Fahrenheit.
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:

def main():
    print "\nThis is a program that converts five celsius values to Fahrenheit"
    ctemp1, ctemp2, ctemp3, ctemp4, ctemp5 = input("\nEnter 5 Celsius temperatures seperated by a comma: ")
    ftemp1 = 9.0 / 5.0 * ctemp1 + 32
    ftemp2 = 9.0 / 5.0 * ctemp2 + 32
    ftemp3 = 9.0 / 5.0 * ctemp3 + 32
    ftemp4 = 9.0 / 5.0 * ctemp4 + 32
    ftemp5 = 9.0 / 5.0 * ctemp5 + 32
    print "------------------------------------------------------------"
    print ctemp1, "degrees Celsius is", ftemp1, "degrees Fahrenheit."
    print ctemp2, "degrees Celsius is", ftemp2, "degrees Fahrenheit."
    print ctemp3, "degrees Celsius is", ftemp3, "degrees Fahrenheit."
    print ctemp4, "degrees Celsius is", ftemp4, "degrees Fahrenheit."
    print ctemp5, "degrees Celsius is", ftemp5, "degrees Fahrenheit."

main()

Help? 8) Thanks

Remove the repetition with a for loop and a list ...

def main():
    print "\nThis program converts five Celsius values to Fahrenheit"
    # cosmetic, shows a line of 65 dashes
    print '-' * 65
    # start with an empty list
    celsius_list = []
    for k in range(1, 6):
        prompt = "Enter Celsius value %d = " % k
        # add the input to a list
        celsius_list.append(input(prompt))
    print '-' * 65
    # now iterate through the list of celsius values
    for c in celsius_list:
        # calculate Fahrenheit
        f = c * 9/5.0 + 32
        print c, "degrees Celsius is", f, "degrees Fahrenheit."

main()

Vegaseat, you really ought to try to give people an idea instead of just writing code for them. People learn better when they can solve the problem themselves. A little direction --a nudge this way or that-- is all that is needed. (It is more satisfying both to the OP and to you who helped.)

Line 10: Never, ever, use input to get user input. That is just plain dangerous. It should read: celsius_list.append([B]raw_input[/B](prompt)) Hope this helps.

[EDIT] Just noticed (again) that you're a mod. I'm probably preaching to the choir. Sorry.

Thanks for the advice! Normally, I do stick with hints, but this one was too simple. To help the OP with learning I added lots of comments.

If you switch from input to raw_input your celsius values will be strings. Ideally you use ...

celsius_list.append(float(raw_input(prompt)))

You are right, input() uses raw_input() and eval() internally, and eval is only one letter away from evil.

Thanks for helping out, it's most appreciated. I'm sure i'll be back in the future for other assistance.

I dont particularly enjoy just being handed the code, I prefer to work at it, it's more fun that way for me and I learn more. But there again, i'm not going to look a gift horse in the mouth ;)

Thanks for your help again.

Sorry again... :$

Nice catch on the type of input.

For extra credits you can work on a generic input function for numeric values (with error trapping).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.