You Are Here:

Community: Developer Discussion Boards

#1 Old Python beginner, need help with Celsius converter - 2008-01-23, 00:53

Join Date: Jan 2008
Posts: 1
sigkill9
Offline
Registered User
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:

Code:
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:
Code:
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? Thanks
Reply With Quote

#2 Old Re: Python beginner, need help with Celsius converter - 2008-01-23, 05:27

Join Date: May 2007
Posts: 2,738
Location: 21.46 N 72.11 E
croozeus's Avatar
croozeus
Offline
Forum Nokia Champion
Hi sigkill9 and welcome to PyS60 Dibo

Your question is more into python rather then python for S60.
You can use loops (while will be suited) to avoid repetative statements as in your code.
This would make your code apt and shorter.
You can also use the array in the loop for ease.
Look in the docs for more details
http://docs.python.org/tut/node6.htm...00000000000000

Hope that helps

Best Regards
Croozeus
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help installing Python libraries on S60 ericroijen Python 11 2009-07-18 11:43
[announce] the first Mobile Python book is now available ! cyke64 Python 1 2008-01-08 10:58
[announce] Mobile Python book soon in Sept 2007 ! cyke64 Python 11 2007-10-03 20:46
Python Beginner tsftsf Python 4 2007-05-27 05:45
Nokia Image Converter davidpurdie General Discussion 0 2004-02-18 16:31

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia