Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modify code
#4
Some points and improvement.
Do not use eval(),convert to int() or float() when needed.
Here the function with docstring better name and f-string(Python 3.6).
# converter.py

def temp_convert():
    '''A program to convert Celsius temps to Fahrenheit'''
    celsius = float(input("what is the celsius temperature? "))
    fahrenheit = 9/5 * celsius + 32
    return(f'{celsius}°C in Celsius is {fahrenheit}°F in Fahrenheit')

if __name__ == '__main__':
    print(temp_convert())
With docstring so do also help() work.
Here a test.
E:\1py_div\div_code
λ ptpython
>>> import converter

>>> help(converter)
Help on module converter:

NAME
    converter - # converter.py

FUNCTIONS
    temp_convert()
        A program to convert Celsius temps to Fahrenheit

FILE
    e:\1py_div\div_code\converter.py

# Run
>>> print(converter.temp_convert())
what is the celsius temperature? 20
20.0°C in Celsius is 68.0°F in Fahrenheit
It's not so much change to turn this into some kind of table,so it's easier to do it rather than give several hints.
The loop range work like this range(start, stop, step).
# temp_table.py

def temp_convert(start_cel, stop_cel, step_temp):
    '''A program to convert Celsius temps to Fahrenheit'''
    for celsius in range(start_cel, stop_cel, step_temp):
        fahrenheit = 9/5 * celsius + 32
        print(f'{celsius}°C --> {fahrenheit}°F')

if __name__ == '__main__':
    start_cel = 0
    stop_cel = 110
    step_temp = 10
    temp_convert(start_cel, stop_cel, step_temp)
Output:
0°C --> 32.0°F 10°C --> 50.0°F 20°C --> 68.0°F 30°C --> 86.0°F 40°C --> 104.0°F 50°C --> 122.0°F 60°C --> 140.0°F 70°C --> 158.0°F 80°C --> 176.0°F 90°C --> 194.0°F 100°C --> 212.0°F
Reply


Messages In This Thread
Modify code - by BlackPimpernel - Mar-31-2018, 03:32 PM
RE: Modify code - by Larz60+ - Mar-31-2018, 09:05 PM
RE: Modify code - by BlackPimpernel - Apr-02-2018, 01:51 PM
RE: Modify code - by snippsat - Apr-02-2018, 03:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Modify code from running program ? samuelbachorik 2 2,536 Jun-26-2020, 08:17 PM
Last Post: samuelbachorik
  How To Modify This Code?? digitalmatic7 6 4,270 Nov-18-2017, 04:49 PM
Last Post: digitalmatic7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020