how do i modify the following program so that it computes and prints a table of celsius temperatures and the fahrenheit equivalent every 10 degrees from 0C to 100C
Converter.py
# A program to convert Celsius temps to Fahrenheit
def main():
print("This program converts Temperature readings")
print("from celsius to fahrenheit")
celsius = eval(input("what is the celsius temperature? "))
fahrenheit = (9/5 * celsius + 32)
print("The temperature is", fahrenheit, "degrees Fahrenheit.")
main()
what's your guess?
Do you know how to use for loops?
(Mar-31-2018, 09:05 PM)Larz60+ Wrote: [ -> ]what's your guess?
Do you know how to use for loops?
im learning. new to this and just trying to teach myself
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