Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 927821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:54:47+00:00 2026-05-15T19:54:47+00:00

I am learning Python (2.7) and to test what I have learned so far

  • 0

I am learning Python (2.7) and to test what I have learned so far I wrote a temperature converter that converts Celsius to Fahrenheit and I wanted to know if my code could be written better to be faster or something more Pythonic. And could someone tell me if there is an actual name for the if __name__ == '__main__': main() (out of curiosity)?

from sys import argv, exit # import argv and exit functions

def to_f(c): # Convert celsius to ferinheight
    temp = (c * 9/5) + 32
    return temp

def to_c(f): # Convert ferinheight to celsius
    temp = (f - 32) * 5/9
    return temp

def main():
    args = argv[1:] # Creates an argument list omitting the omitting the [0] element
    if len(argv) < 2: exit(1) # If less than two arguments
    if args[0] == '-f': # If the first argument is -f
        print args[1], 'ferinheight is', str(to_c(int(args[1]))), 'celsius'
    elif args[0] == '-c': # If the first argument is -c
        print args[1], 'celsius is', str(to_f(int(args[1]))), 'ferinheight'
    else: exit(1)

if __name__ == '__main__':
    main()

http://pastebin.com/rjeNikDt

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:54:48+00:00Added an answer on May 15, 2026 at 7:54 pm
    import sys
    
    def to_f(c): # Convert celsius to fahrenheit
        return (c * 9/5) + 32
    
    def to_c(f): # Convert fahrenheit to celsius
        return (f - 32) * 5/9
    
    def convert(args):
        if len(args) < 2:
            return 1 # If less than two arguments
        t = args[1]
        if args[0] == '-f': # If the first argument is -f
            print "%s Fahrenheit is %s Celsius" % (t, to_c(int(t)))
            return 0
        elif args[0] == '-c': # If the first argument is -c
            print "%s Celsius is %s Fahrenheit" % (t, to_f(int(t)))
            return 0
        else:
            return 1
    
    if __name__ == '__main__':
        sys.exit(convert(sys.argv[1:]))
    

    What I did:

    1. Changed the name of main() to convert()
    2. Pass the arguments to convert() explicitly
    3. Change calls to exit() to returns, and call exit() in the main clause.
    4. You were checking argv for length 2, when you should have been checking args.
    5. The to_c and to_f functions don’t need a temp variable, just return the expression.
    6. Although others are right that you can just put the main() function at the top level, it is good form to use the if __name__ style, so that you could import this module and use the functions in other code.
    7. String formatting is nicer than intermixing strings and values in the print statement.
    8. args[1] appears enough that I assigned it to t for brevity.
    9. I prefer importing sys, and using sys.argv, for example.
    10. I always put dependent clauses on new lines, never if blah: doit()
    11. Fix the spelling of Fahrenheit
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning python with a book that teaches by creating games. I have to
I'm learning Python and trying to test some rest web_service applications. From the below
I have just started learning python version 3 and trying to create a file
I have only started learning python recently. I would still be considered a beginner.
I just started using/learning Python and have some questions. I have a text file
I have only started learning Python recently. Let me explain what I am trying
I am learning python at the moment and I have a text file with
I'm interested in learning python to have access to a more agile language for
im currently learning python (in the very begining), so I still have some doubts
I am learning python at college. I have got a very stupid question. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.