Programming Fundamentals/Arrays/Python3

arrays.py

edit
# This program uses arrays to display temperature conversion tables 
# and temperature as an array subscript to find a given conversion.
#
# References:
#     https://www.mathsisfun.com/temperature-conversion.html
#     https://en.wikibooks.org/wiki/Python_Programming

def build_c(size):
    c = []
    for index in range(0, size + 1):
        value = index * 9 / 5 + 32
        c.append(value)
    return c
    
def build_f(size):
    f = []
    for index in range(0, size + 1):
        value = (index - 32) * 5 / 9
        f.append(value)
    return f
    
def display_array(name, array):
    for index in range(0, len(array)):
        print(name + "[" + str(index) + "] = " + str(array[index]))

def find_temperature(c, f):
    size = minimum(len(c), len(f))
    while True:
        print("Enter a temperature between 0 and " + str((size - 1)))
        temp = int(input())
        if not(temp < 0 or temp > size - 1): 
            break
    print(str(temp) + "° Celsius is " + str(c[temp]) + "° Fahrenheit")
    print(str(temp) + "° Fahrenheit is " + str(f[temp]) + "° Celsius")

def minimum(value1, value2):
    if value1 < value2:
        result = value1
    else:
        result = value2
    return result

def main():
    c = build_c(100)
    f = build_f(212)
    display_array("C", c)
    display_array("F", f)
    find_temperature(c, f)

main()

Try It

edit

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

edit