Programming Fundamentals/Loops/Python3: Difference between revisions

From Wikiversity
Jump to navigation Jump to search
Content deleted Content added
Creating
(No difference)

Revision as of 04:30, 8 February 2017

conditions.py

# This program displays a temperature conversion table showing Fahrenheit
# temperatures from 0 to 100, in increments of 10, and the corresponding 
# Celsius temperatures using While, For, and Do loops.

def while_loop():
    print("F°    C°")
    f = 0
    while f <= 100:
        c = (f - 32) * 5 / 9
        print(str(f) + " = " + str(c))
        f += 10

def for_loop():
    print("F°    C°")
    for f in range(0, 100, 10):
        c = (f - 32) * 5 / 9
        print(str(f) + " = " + str(c))

def do_loop():
    print("F°    C°")
    f = 0
    while True:
        c = (f - 32) * 5 / 9
        print(str(f) + " = " + str(c))
        f += 10
        if not(f <= 100): 
            break
        
def main():
    while_loop()
    for_loop()
    do_loop()

main()

Try It

Copy and paste the code above into one of the following free online development environments or use your own Python3 compiler / interpreter / IDE.

See Also