Python 3 - simple temperature program version 1.3Python 3 - simple temperature programTemperature conversion in CPython Unit Coversion Code Optimization in terms of Space and Time ComplexityGenerating formatted multiplication tables in C++Simple temperature converter in CSimple Temperature Converter 2 in CTemperature converter programTemperature conversion codeXOR cipher C programMedian of a Vector assignmentPython 3 - simple temperature program

Can a player choose to add detail and flavor to their character's spells and abilities?

Which "exotic salt" can lower water's freezing point by –70 °C?

Dimmer switch not connected to ground

While drilling into kitchen wall, hit a wire - any advice?

Make me a minimum magic sum

Is there precedent or are there procedures for a US president refusing to concede to an electoral defeat?

Hostile Divisor Numbers

Gerrymandering Puzzle - Rig the Election

Why is the blank symbol not considered part of the input alphabet of a Turing machine?

Collision domain question

How long does it take a postcard to get from USA to Germany?

What does the coin flipping before dying mean?

What is monoid homomorphism exactly?

TIP120 Transistor + Solenoid Failing Randomly

Can an Iranian citizen enter the USA on a Dutch passport?

How is Pauli's exclusion principle still valid in these cases?

Sci-fi/fantasy book - ships on steel runners skating across ice sheets

Picking a theme as a discovery writer

Subnumcases as a part of align

Given a safe domain, are subdirectories safe as well?

How to do this Padovan spiral using Mathematica?

What's the 2-minute timer on mobile Deutsche Bahn tickets?

Debian 9 server no sshd in auth.log

Why would a military not separate its forces into different branches?



Python 3 - simple temperature program version 1.3


Python 3 - simple temperature programTemperature conversion in CPython Unit Coversion Code Optimization in terms of Space and Time ComplexityGenerating formatted multiplication tables in C++Simple temperature converter in CSimple Temperature Converter 2 in CTemperature converter programTemperature conversion codeXOR cipher C programMedian of a Vector assignmentPython 3 - simple temperature program






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1












$begingroup$


Thank you to everyone for the feedback provided to the initial version of this program posted here.



Please find below the newest version, revised based on the comments provided, for review and comment on how I can further improve my coding.



I have removed the portion of the program that checked if the provided value was a legitimate temperature, as this was useless code only showing that I had recently learned what the opposite of absolute zero was....who cares, and who cares what value the user wants to convert!



I look forward to your feedback.



#!/usr/bin/python
"""

Program: Temperature Conversion (C to F, or F to C)
Date: 05 May 2019
Version: 1.2
Author: Jason P. Karle

Remark: This program was inspired by a Python exercise that
asks you to create a program that will convert one Celsius value to Fahrenheit;
so a program that can be executed with three lines of code.

However, I wanted to make something that would allow the user to
convert to and from either C of F, and do so multiple times, until the user
decides to end the program. This was also an exercise for me to
advance not only my code skills, but how I structure a program.

version history:
1.0 Initial draft
1.1 Correction of runtime; posted to StackExchange for feedback
1.2 Re-coded based on feedback; trying to improve flow control
"""


def read_selection():
selection = input('''Welcome to the temperature conversion program!

Please make a selection:

c to convert from Celcius to Fahrenheit;
f to convert from Fahrenheit to Celsius; or
q to quit the program.

Enter your selection: ''').lower()
return selection


def value_input(selection):
value = input('''nPlease enter the temperature you
want to convert: ''')
try:
value = float(value)
except ValueError:
print('That is not a number!n')
else:
if selection == 'c':
convert_c2f(value)
else:
convert_f2c(value)
# return value


def convert_c2f(value):
print(f'The answer is: (value * (9/5)) + 32°Fn')
return


def convert_f2c(value):
print(f'The answer is: (value-32) * (5/9)°Cn')
return


def main():
while True:
selection = read_selection()
if selection == 'q':
return
elif selection == 'c' or selection == 'f':
value_input(selection)
'''convert_c2f()
elif selection == 'f':
convert_f2C()'''
else:
print('Invalid selction. Try again.n')


if __name__ == '__main__':
main()









share|improve this question









New contributor




J Karle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$


















    1












    $begingroup$


    Thank you to everyone for the feedback provided to the initial version of this program posted here.



    Please find below the newest version, revised based on the comments provided, for review and comment on how I can further improve my coding.



    I have removed the portion of the program that checked if the provided value was a legitimate temperature, as this was useless code only showing that I had recently learned what the opposite of absolute zero was....who cares, and who cares what value the user wants to convert!



    I look forward to your feedback.



    #!/usr/bin/python
    """

    Program: Temperature Conversion (C to F, or F to C)
    Date: 05 May 2019
    Version: 1.2
    Author: Jason P. Karle

    Remark: This program was inspired by a Python exercise that
    asks you to create a program that will convert one Celsius value to Fahrenheit;
    so a program that can be executed with three lines of code.

    However, I wanted to make something that would allow the user to
    convert to and from either C of F, and do so multiple times, until the user
    decides to end the program. This was also an exercise for me to
    advance not only my code skills, but how I structure a program.

    version history:
    1.0 Initial draft
    1.1 Correction of runtime; posted to StackExchange for feedback
    1.2 Re-coded based on feedback; trying to improve flow control
    """


    def read_selection():
    selection = input('''Welcome to the temperature conversion program!

    Please make a selection:

    c to convert from Celcius to Fahrenheit;
    f to convert from Fahrenheit to Celsius; or
    q to quit the program.

    Enter your selection: ''').lower()
    return selection


    def value_input(selection):
    value = input('''nPlease enter the temperature you
    want to convert: ''')
    try:
    value = float(value)
    except ValueError:
    print('That is not a number!n')
    else:
    if selection == 'c':
    convert_c2f(value)
    else:
    convert_f2c(value)
    # return value


    def convert_c2f(value):
    print(f'The answer is: (value * (9/5)) + 32°Fn')
    return


    def convert_f2c(value):
    print(f'The answer is: (value-32) * (5/9)°Cn')
    return


    def main():
    while True:
    selection = read_selection()
    if selection == 'q':
    return
    elif selection == 'c' or selection == 'f':
    value_input(selection)
    '''convert_c2f()
    elif selection == 'f':
    convert_f2C()'''
    else:
    print('Invalid selction. Try again.n')


    if __name__ == '__main__':
    main()









    share|improve this question









    New contributor




    J Karle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      1












      1








      1


      1



      $begingroup$


      Thank you to everyone for the feedback provided to the initial version of this program posted here.



      Please find below the newest version, revised based on the comments provided, for review and comment on how I can further improve my coding.



      I have removed the portion of the program that checked if the provided value was a legitimate temperature, as this was useless code only showing that I had recently learned what the opposite of absolute zero was....who cares, and who cares what value the user wants to convert!



      I look forward to your feedback.



      #!/usr/bin/python
      """

      Program: Temperature Conversion (C to F, or F to C)
      Date: 05 May 2019
      Version: 1.2
      Author: Jason P. Karle

      Remark: This program was inspired by a Python exercise that
      asks you to create a program that will convert one Celsius value to Fahrenheit;
      so a program that can be executed with three lines of code.

      However, I wanted to make something that would allow the user to
      convert to and from either C of F, and do so multiple times, until the user
      decides to end the program. This was also an exercise for me to
      advance not only my code skills, but how I structure a program.

      version history:
      1.0 Initial draft
      1.1 Correction of runtime; posted to StackExchange for feedback
      1.2 Re-coded based on feedback; trying to improve flow control
      """


      def read_selection():
      selection = input('''Welcome to the temperature conversion program!

      Please make a selection:

      c to convert from Celcius to Fahrenheit;
      f to convert from Fahrenheit to Celsius; or
      q to quit the program.

      Enter your selection: ''').lower()
      return selection


      def value_input(selection):
      value = input('''nPlease enter the temperature you
      want to convert: ''')
      try:
      value = float(value)
      except ValueError:
      print('That is not a number!n')
      else:
      if selection == 'c':
      convert_c2f(value)
      else:
      convert_f2c(value)
      # return value


      def convert_c2f(value):
      print(f'The answer is: (value * (9/5)) + 32°Fn')
      return


      def convert_f2c(value):
      print(f'The answer is: (value-32) * (5/9)°Cn')
      return


      def main():
      while True:
      selection = read_selection()
      if selection == 'q':
      return
      elif selection == 'c' or selection == 'f':
      value_input(selection)
      '''convert_c2f()
      elif selection == 'f':
      convert_f2C()'''
      else:
      print('Invalid selction. Try again.n')


      if __name__ == '__main__':
      main()









      share|improve this question









      New contributor




      J Karle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      Thank you to everyone for the feedback provided to the initial version of this program posted here.



      Please find below the newest version, revised based on the comments provided, for review and comment on how I can further improve my coding.



      I have removed the portion of the program that checked if the provided value was a legitimate temperature, as this was useless code only showing that I had recently learned what the opposite of absolute zero was....who cares, and who cares what value the user wants to convert!



      I look forward to your feedback.



      #!/usr/bin/python
      """

      Program: Temperature Conversion (C to F, or F to C)
      Date: 05 May 2019
      Version: 1.2
      Author: Jason P. Karle

      Remark: This program was inspired by a Python exercise that
      asks you to create a program that will convert one Celsius value to Fahrenheit;
      so a program that can be executed with three lines of code.

      However, I wanted to make something that would allow the user to
      convert to and from either C of F, and do so multiple times, until the user
      decides to end the program. This was also an exercise for me to
      advance not only my code skills, but how I structure a program.

      version history:
      1.0 Initial draft
      1.1 Correction of runtime; posted to StackExchange for feedback
      1.2 Re-coded based on feedback; trying to improve flow control
      """


      def read_selection():
      selection = input('''Welcome to the temperature conversion program!

      Please make a selection:

      c to convert from Celcius to Fahrenheit;
      f to convert from Fahrenheit to Celsius; or
      q to quit the program.

      Enter your selection: ''').lower()
      return selection


      def value_input(selection):
      value = input('''nPlease enter the temperature you
      want to convert: ''')
      try:
      value = float(value)
      except ValueError:
      print('That is not a number!n')
      else:
      if selection == 'c':
      convert_c2f(value)
      else:
      convert_f2c(value)
      # return value


      def convert_c2f(value):
      print(f'The answer is: (value * (9/5)) + 32°Fn')
      return


      def convert_f2c(value):
      print(f'The answer is: (value-32) * (5/9)°Cn')
      return


      def main():
      while True:
      selection = read_selection()
      if selection == 'q':
      return
      elif selection == 'c' or selection == 'f':
      value_input(selection)
      '''convert_c2f()
      elif selection == 'f':
      convert_f2C()'''
      else:
      print('Invalid selction. Try again.n')


      if __name__ == '__main__':
      main()






      python beginner python-3.x unit-conversion






      share|improve this question









      New contributor




      J Karle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      J Karle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 3 hours ago









      200_success

      132k20159424




      132k20159424






      New contributor




      J Karle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 hours ago









      J KarleJ Karle

      688




      688




      New contributor




      J Karle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      J Karle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      J Karle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          3 Answers
          3






          active

          oldest

          votes


















          3












          $begingroup$

          You're not following PEP8 and you still have a spaghetti mind-set. Each function should have a single responsibility.



          value_input however is in charge of:



          1. Asking and validating user input.

          2. Handling how to convert the input.

          3. Convert and display the input.



          This should instead only perform the first task I've said above. After this you should have the calling code perform 2 and 3.



          convert_c2f also is responsible for two things converting and displaying the input.



          It can be seen in main that you were originally doing something better than you have now, so it's unclear why you changed this.



          def read_selection():
          return input('''Welcome to the temperature conversion program!

          Please make a selection:

          c to convert from Celcius to Fahrenheit;
          f to convert from Fahrenheit to Celsius; or
          q to quit the program.

          Enter your selection: ''')


          def float_input():
          value = input('''nPlease enter the temperature you
          want to convert: ''')
          try:
          return float(value)
          except ValueError:
          print('That is not a number!n')


          def convert_c2f(value):
          return (value * (9 / 5)) + 32


          def convert_f2c(value):
          return (value - 32) * (5 / 9)


          def main():
          while True:
          selection = read_selection().lower()
          if selection == 'q':
          return
          elif selection == 'c' or selection == 'f':
          value = float_input(selection)
          if selection == 'c':
          converted = convert_c2f(value)
          print(f'The answer is: converted°Fn')
          else:
          converted = convert_f2c(value)
          print(f'The answer is: converted°Cn')
          else:
          print('Invalid selction. Try again.n')





          share|improve this answer









          $endgroup$












          • $begingroup$
            @AlexV My answer doesn't contain the shebang. That could make a good answer however.
            $endgroup$
            – Peilonrayz
            3 hours ago


















          2












          $begingroup$

          Your conversion functions have two notable things:



          • You're printing the result directly. Don't do this. For toy programs like this, this is ok. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything.


          • You're putting an empty return at the end. This is redundant though and unnecessary.


          I would have the functions return the converted value, and print it at the call site:



          def value_input(selection):
          value = input('''nPlease enter the temperature you
          want to convert: ''')
          try:
          value = float(value)

          except ValueError:
          print('That is not a number!n')

          else:
          is_celsius = selection == 'c' # Save if we're converting to Celsius or not

          new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
          unit_str = "C" if is_celsius else "F"

          print(f'The answer is: new_value°unit_str')


          def convert_c2f(value):
          return (value * (9 / 5)) + 32

          def convert_f2c(value):
          return (value - 32) * (5 / 9)


          I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. I get an odd thrill out of reducing duplication. Do whatever you feel is more readable.






          share|improve this answer











          $endgroup$




















            1












            $begingroup$

            @Peilonrayz and @Carcigenicate have great feedback you should definitely follow.



            Since you are using Python 3 features like f-string, #!/usr/bin/env python3 should be used as shebang line.






            share|improve this answer









            $endgroup$













              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "196"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );






              J Karle is a new contributor. Be nice, and check out our Code of Conduct.









              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f219767%2fpython-3-simple-temperature-program-version-1-3%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3












              $begingroup$

              You're not following PEP8 and you still have a spaghetti mind-set. Each function should have a single responsibility.



              value_input however is in charge of:



              1. Asking and validating user input.

              2. Handling how to convert the input.

              3. Convert and display the input.



              This should instead only perform the first task I've said above. After this you should have the calling code perform 2 and 3.



              convert_c2f also is responsible for two things converting and displaying the input.



              It can be seen in main that you were originally doing something better than you have now, so it's unclear why you changed this.



              def read_selection():
              return input('''Welcome to the temperature conversion program!

              Please make a selection:

              c to convert from Celcius to Fahrenheit;
              f to convert from Fahrenheit to Celsius; or
              q to quit the program.

              Enter your selection: ''')


              def float_input():
              value = input('''nPlease enter the temperature you
              want to convert: ''')
              try:
              return float(value)
              except ValueError:
              print('That is not a number!n')


              def convert_c2f(value):
              return (value * (9 / 5)) + 32


              def convert_f2c(value):
              return (value - 32) * (5 / 9)


              def main():
              while True:
              selection = read_selection().lower()
              if selection == 'q':
              return
              elif selection == 'c' or selection == 'f':
              value = float_input(selection)
              if selection == 'c':
              converted = convert_c2f(value)
              print(f'The answer is: converted°Fn')
              else:
              converted = convert_f2c(value)
              print(f'The answer is: converted°Cn')
              else:
              print('Invalid selction. Try again.n')





              share|improve this answer









              $endgroup$












              • $begingroup$
                @AlexV My answer doesn't contain the shebang. That could make a good answer however.
                $endgroup$
                – Peilonrayz
                3 hours ago















              3












              $begingroup$

              You're not following PEP8 and you still have a spaghetti mind-set. Each function should have a single responsibility.



              value_input however is in charge of:



              1. Asking and validating user input.

              2. Handling how to convert the input.

              3. Convert and display the input.



              This should instead only perform the first task I've said above. After this you should have the calling code perform 2 and 3.



              convert_c2f also is responsible for two things converting and displaying the input.



              It can be seen in main that you were originally doing something better than you have now, so it's unclear why you changed this.



              def read_selection():
              return input('''Welcome to the temperature conversion program!

              Please make a selection:

              c to convert from Celcius to Fahrenheit;
              f to convert from Fahrenheit to Celsius; or
              q to quit the program.

              Enter your selection: ''')


              def float_input():
              value = input('''nPlease enter the temperature you
              want to convert: ''')
              try:
              return float(value)
              except ValueError:
              print('That is not a number!n')


              def convert_c2f(value):
              return (value * (9 / 5)) + 32


              def convert_f2c(value):
              return (value - 32) * (5 / 9)


              def main():
              while True:
              selection = read_selection().lower()
              if selection == 'q':
              return
              elif selection == 'c' or selection == 'f':
              value = float_input(selection)
              if selection == 'c':
              converted = convert_c2f(value)
              print(f'The answer is: converted°Fn')
              else:
              converted = convert_f2c(value)
              print(f'The answer is: converted°Cn')
              else:
              print('Invalid selction. Try again.n')





              share|improve this answer









              $endgroup$












              • $begingroup$
                @AlexV My answer doesn't contain the shebang. That could make a good answer however.
                $endgroup$
                – Peilonrayz
                3 hours ago













              3












              3








              3





              $begingroup$

              You're not following PEP8 and you still have a spaghetti mind-set. Each function should have a single responsibility.



              value_input however is in charge of:



              1. Asking and validating user input.

              2. Handling how to convert the input.

              3. Convert and display the input.



              This should instead only perform the first task I've said above. After this you should have the calling code perform 2 and 3.



              convert_c2f also is responsible for two things converting and displaying the input.



              It can be seen in main that you were originally doing something better than you have now, so it's unclear why you changed this.



              def read_selection():
              return input('''Welcome to the temperature conversion program!

              Please make a selection:

              c to convert from Celcius to Fahrenheit;
              f to convert from Fahrenheit to Celsius; or
              q to quit the program.

              Enter your selection: ''')


              def float_input():
              value = input('''nPlease enter the temperature you
              want to convert: ''')
              try:
              return float(value)
              except ValueError:
              print('That is not a number!n')


              def convert_c2f(value):
              return (value * (9 / 5)) + 32


              def convert_f2c(value):
              return (value - 32) * (5 / 9)


              def main():
              while True:
              selection = read_selection().lower()
              if selection == 'q':
              return
              elif selection == 'c' or selection == 'f':
              value = float_input(selection)
              if selection == 'c':
              converted = convert_c2f(value)
              print(f'The answer is: converted°Fn')
              else:
              converted = convert_f2c(value)
              print(f'The answer is: converted°Cn')
              else:
              print('Invalid selction. Try again.n')





              share|improve this answer









              $endgroup$



              You're not following PEP8 and you still have a spaghetti mind-set. Each function should have a single responsibility.



              value_input however is in charge of:



              1. Asking and validating user input.

              2. Handling how to convert the input.

              3. Convert and display the input.



              This should instead only perform the first task I've said above. After this you should have the calling code perform 2 and 3.



              convert_c2f also is responsible for two things converting and displaying the input.



              It can be seen in main that you were originally doing something better than you have now, so it's unclear why you changed this.



              def read_selection():
              return input('''Welcome to the temperature conversion program!

              Please make a selection:

              c to convert from Celcius to Fahrenheit;
              f to convert from Fahrenheit to Celsius; or
              q to quit the program.

              Enter your selection: ''')


              def float_input():
              value = input('''nPlease enter the temperature you
              want to convert: ''')
              try:
              return float(value)
              except ValueError:
              print('That is not a number!n')


              def convert_c2f(value):
              return (value * (9 / 5)) + 32


              def convert_f2c(value):
              return (value - 32) * (5 / 9)


              def main():
              while True:
              selection = read_selection().lower()
              if selection == 'q':
              return
              elif selection == 'c' or selection == 'f':
              value = float_input(selection)
              if selection == 'c':
              converted = convert_c2f(value)
              print(f'The answer is: converted°Fn')
              else:
              converted = convert_f2c(value)
              print(f'The answer is: converted°Cn')
              else:
              print('Invalid selction. Try again.n')






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 3 hours ago









              PeilonrayzPeilonrayz

              27k341113




              27k341113











              • $begingroup$
                @AlexV My answer doesn't contain the shebang. That could make a good answer however.
                $endgroup$
                – Peilonrayz
                3 hours ago
















              • $begingroup$
                @AlexV My answer doesn't contain the shebang. That could make a good answer however.
                $endgroup$
                – Peilonrayz
                3 hours ago















              $begingroup$
              @AlexV My answer doesn't contain the shebang. That could make a good answer however.
              $endgroup$
              – Peilonrayz
              3 hours ago




              $begingroup$
              @AlexV My answer doesn't contain the shebang. That could make a good answer however.
              $endgroup$
              – Peilonrayz
              3 hours ago













              2












              $begingroup$

              Your conversion functions have two notable things:



              • You're printing the result directly. Don't do this. For toy programs like this, this is ok. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything.


              • You're putting an empty return at the end. This is redundant though and unnecessary.


              I would have the functions return the converted value, and print it at the call site:



              def value_input(selection):
              value = input('''nPlease enter the temperature you
              want to convert: ''')
              try:
              value = float(value)

              except ValueError:
              print('That is not a number!n')

              else:
              is_celsius = selection == 'c' # Save if we're converting to Celsius or not

              new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
              unit_str = "C" if is_celsius else "F"

              print(f'The answer is: new_value°unit_str')


              def convert_c2f(value):
              return (value * (9 / 5)) + 32

              def convert_f2c(value):
              return (value - 32) * (5 / 9)


              I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. I get an odd thrill out of reducing duplication. Do whatever you feel is more readable.






              share|improve this answer











              $endgroup$

















                2












                $begingroup$

                Your conversion functions have two notable things:



                • You're printing the result directly. Don't do this. For toy programs like this, this is ok. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything.


                • You're putting an empty return at the end. This is redundant though and unnecessary.


                I would have the functions return the converted value, and print it at the call site:



                def value_input(selection):
                value = input('''nPlease enter the temperature you
                want to convert: ''')
                try:
                value = float(value)

                except ValueError:
                print('That is not a number!n')

                else:
                is_celsius = selection == 'c' # Save if we're converting to Celsius or not

                new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
                unit_str = "C" if is_celsius else "F"

                print(f'The answer is: new_value°unit_str')


                def convert_c2f(value):
                return (value * (9 / 5)) + 32

                def convert_f2c(value):
                return (value - 32) * (5 / 9)


                I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. I get an odd thrill out of reducing duplication. Do whatever you feel is more readable.






                share|improve this answer











                $endgroup$















                  2












                  2








                  2





                  $begingroup$

                  Your conversion functions have two notable things:



                  • You're printing the result directly. Don't do this. For toy programs like this, this is ok. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything.


                  • You're putting an empty return at the end. This is redundant though and unnecessary.


                  I would have the functions return the converted value, and print it at the call site:



                  def value_input(selection):
                  value = input('''nPlease enter the temperature you
                  want to convert: ''')
                  try:
                  value = float(value)

                  except ValueError:
                  print('That is not a number!n')

                  else:
                  is_celsius = selection == 'c' # Save if we're converting to Celsius or not

                  new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
                  unit_str = "C" if is_celsius else "F"

                  print(f'The answer is: new_value°unit_str')


                  def convert_c2f(value):
                  return (value * (9 / 5)) + 32

                  def convert_f2c(value):
                  return (value - 32) * (5 / 9)


                  I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. I get an odd thrill out of reducing duplication. Do whatever you feel is more readable.






                  share|improve this answer











                  $endgroup$



                  Your conversion functions have two notable things:



                  • You're printing the result directly. Don't do this. For toy programs like this, this is ok. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything.


                  • You're putting an empty return at the end. This is redundant though and unnecessary.


                  I would have the functions return the converted value, and print it at the call site:



                  def value_input(selection):
                  value = input('''nPlease enter the temperature you
                  want to convert: ''')
                  try:
                  value = float(value)

                  except ValueError:
                  print('That is not a number!n')

                  else:
                  is_celsius = selection == 'c' # Save if we're converting to Celsius or not

                  new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
                  unit_str = "C" if is_celsius else "F"

                  print(f'The answer is: new_value°unit_str')


                  def convert_c2f(value):
                  return (value * (9 / 5)) + 32

                  def convert_f2c(value):
                  return (value - 32) * (5 / 9)


                  I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. I get an odd thrill out of reducing duplication. Do whatever you feel is more readable.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 2 hours ago

























                  answered 3 hours ago









                  CarcigenicateCarcigenicate

                  4,50411735




                  4,50411735





















                      1












                      $begingroup$

                      @Peilonrayz and @Carcigenicate have great feedback you should definitely follow.



                      Since you are using Python 3 features like f-string, #!/usr/bin/env python3 should be used as shebang line.






                      share|improve this answer









                      $endgroup$

















                        1












                        $begingroup$

                        @Peilonrayz and @Carcigenicate have great feedback you should definitely follow.



                        Since you are using Python 3 features like f-string, #!/usr/bin/env python3 should be used as shebang line.






                        share|improve this answer









                        $endgroup$















                          1












                          1








                          1





                          $begingroup$

                          @Peilonrayz and @Carcigenicate have great feedback you should definitely follow.



                          Since you are using Python 3 features like f-string, #!/usr/bin/env python3 should be used as shebang line.






                          share|improve this answer









                          $endgroup$



                          @Peilonrayz and @Carcigenicate have great feedback you should definitely follow.



                          Since you are using Python 3 features like f-string, #!/usr/bin/env python3 should be used as shebang line.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 3 hours ago









                          AlexVAlexV

                          1,851620




                          1,851620




















                              J Karle is a new contributor. Be nice, and check out our Code of Conduct.









                              draft saved

                              draft discarded


















                              J Karle is a new contributor. Be nice, and check out our Code of Conduct.












                              J Karle is a new contributor. Be nice, and check out our Code of Conduct.











                              J Karle is a new contributor. Be nice, and check out our Code of Conduct.














                              Thanks for contributing an answer to Code Review Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              Use MathJax to format equations. MathJax reference.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f219767%2fpython-3-simple-temperature-program-version-1-3%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                              Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                              François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480