Write a Python program to show the temperatures starting at zero degrees Celsius, up to 100 degrees Celsius, in steps of 2 degrees, with the corresponding Fahreneit temperature shown in the next column. Temperatures should be shown as floating point numbers to two decimal places. Use a for loop for displaying each line of the table and the following formula for the temperature conversion:

Example Program run:
C F
0.00 32.00
2.00 35.60
4.00 39.20
6.00 42.80
100.00 212.00
_____________________________________________________
I have written the program and it works, but the problem i have is, my professor only wants the even number and its corresponding farenheit values to be shows, how do i do that?

This is the code I have typed in for this assignment and it works, just need to know to display the range of even numbers and its corresponding Fahrenheit values only, my code:

while 1:  
    print ("C" + '\t' +"F")
    celsius = 0
    for celsius in range(0, 101):
        fahrenheit = 1.8 * celsius + 32 
        print ('%0.2f %0.2f' % (celsius,fahrenheit))
    answer = input("\nWould you like to run this program again? y/n\n")
    if answer == 'N' or answer == 'n':
        break
print ()
print ()
print ("Thank you for using this program.")
print ("Goodbye...")

Thanks in advance.

Tony

Moderator's note:
Please use code tags with your Python code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.

[code=python]
your Python code here

[/code]

Dani AI

Generated

Good progress in the thread — both ’s parity-check and ’s step-argument point to the common ways to produce only the even Celsius values. A few concise refinements improve readability, portability between Python versions, and numeric presentation: isolate the conversion in a small function, format columns with fixed widths, handle repeated runs with a clear prompt check, and catch interrupts so the program exits cleanly. A note on numeric behavior: binary floats can display tiny artifacts (e.g., 39.1999999) even though formatting shows two decimals; the Decimal module avoids that when exact decimal representation is important.

Historic Python differences that are relevant: in Python 2 integer division can change results if literals like 9/5 are used (float literals or from future import division avoid that), and input() behaves differently in Python 2 vs 3 (raw_input in Py2). Also, preserving indentation when posting code (as reminded) keeps examples runnable for others.

A compact, modern example (Python 3) that keeps logic separated, produces aligned two-decimal columns, and avoids binary-float display issues by using Decimal:

from decimal import Decimal, getcontext

getcontext().prec = 10

def even_celsius():
    return (i * 2 for i in range(51))   # 0, 2, ..., 100

def c_to_f(c_dec):
    return c_dec * Decimal('9') / Decimal('5') + Decimal('32')

def print_table():
    print("    C       F")
    for c in even_celsius():
        c_dec = Decimal(c)
        f = c_to_f(c_dec)
        print(f"{c_dec:6.2f} {f:8.2f}")

if __name__ == "__main__":
    try:
        while True:
            print_table()
            if input("Run again? [y/N] ").strip().lower() != "y":
                break
    except (KeyboardInterrupt, EOFError):
        print("\nExiting.")

Recommended Answers

All 5 Replies

Use a remainder modulator:

while 1:
print ("C" + '\t' +"F")
celsius = 0
for celsius in range(0, 101):
fahrenheit = 1.8 * celsius + 32
if celsius % 2 == 0:
print ('%0.2f %0.2f' % (celsius,fahrenheit))
else:
pass
answer = input("\nWould you like to run this program again? y/n\n")
if answer == 'N' or answer == 'n':
break
print ()
print ()
print ("Thank you for using this program.")
print ("Goodbye...")

This might need a little cleaning up, but should eliminate your odd numbers.

I tried to include indentation in this reply, but I now see that everything was left aligned. You should already understand tabs and indentation, but the if and else statements should be single tabbed and their respective executions should be double tabbed.

Use a remainder modulator:

while 1:
print ("C" + '\t' +"F")
celsius = 0
for celsius in range(0, 101):
fahrenheit = 1.8 * celsius + 32
if celsius % 2 == 0:
print ('%0.2f %0.2f' % (celsius,fahrenheit))
else:
pass
answer = input("\nWould you like to run this program again? y/n\n")
if answer == 'N' or answer == 'n':
break
print ()
print ()
print ("Thank you for using this program.")
print ("Goodbye...")

This might need a little cleaning up, but should eliminate your odd numbers.

Thank you so much for ur help, i managed to figure it out and it works like a flash, thanks so much....

Tony

To the both of you:

In order to preserve indentation use code tags while posting code in this forum like so:
[code=python] # Code goes inside here

[/code]

Just a note to remind you that range(start, stop, step) has a step option, so you could have used ...

for celsius in range(0, 101, 2):
    fahrenheit = 1.8 * celsius + 32 
    print ('%0.2f %0.2f' % (celsius,fahrenheit))

All in all, nice clean coding style. Hope you enjoy your Python experience.

commented: haha brilliantly clean, simple solution +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.