I will keep it simple. I need to modify my code so that it will compute and print a table of celsius temperatures and the Fahrenheit equivalents every 10 degrees from 0C to 100C

Here is what I have.

# convert.py
# A program to convert Celsius to Fahrenheit

def main():
    celsius = eval(input("What is the Celsius temperature? "))
    fahrenheit = 9/5 * celsius + 32
    print ("The temperature is", farenheit, "degrees Fahrenheit.")

1 Answers

0
Seraf On

You can use a loop, range and a formatting string:

for celcius in range(0, 101, 10):
    print(f'Celcius: {celcius}, Fahrenheit: {celcius * 9 / 5 + 32}')

Range explanation: range(0, 101, 10) will start at 0, finish at 100 (101 is excluded) and have jumps of 10 between each element. If you want to investigate more into range, you can cast list to it and have a representation of what happens like this:

>>> list(range(0, 101, 10))
>>> [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100] 

So celcius will take those values in the iterations of for celcius in range(0, 101, 10)

Then all what's left to do is to print the value of celcius and fehrenheit. Using the format string (f'') You have: f'Celcius: {celcius}, Fahrenheit: {celcius * 9 / 5 + 32}', the formatter will evaluate what's in the curly brackets ({}) the build the string with that.