Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Typeerror
#1
For this assignment we are to make a program that prompts the user for two temperature values between -20 and 50. The second one must be bigger than the first. Then two table looking things are to be formatted. The first one has all of the wind speed values from 5mph - 40 by 5s. The second table displays the values for the temperatures after the heat index is applied. If the fahrenheit temp is over 50 for wind chill, and "X" is displayed there. If the heat index temp is under 80, the same thing happens.

They look something like this
wind chill
celsius--- fahrenheit--- 5mph--- 10mph--- 15mph.....
12------------ 50------------ X------------ X------------ X
(Without the dashes of course)

Also some times for my wind chill index the numbers do something weird. If you put in the numbers 12 and 15, at number 13 you'll see what I mean. I'd also like to know whats happening there.

The main problem I'm having is with my second table. I am only attempting to print the first 3 columns, byt I'm getting
TypeError: 'int' object is not iterable
I'd like to know why cause I have no idea.
Thanks for reading all that
this is my main function:
from functions import compute_wind_chill, compute_heat_index
xy_list = []
fahr_list = []
if __name__ == '__main__':
    x, y = [int(x) for x in input("Enter two numbers here seperated by space: ").split()]
    x = int(x)
    y = int(y)
    y = y + 1
    Twc_list5, Twc_list10, Twc_list15, Twc_list20, Twc_list25, Twc_list30, Twc_list35, Twc_list40 = compute_wind_chill(x, y)
    Hi_list40 = compute_heat_index(x, y)
    for i in range(x, y):
        xy_list.append(i)
    for i in xy_list:
        fahrx = (i * 1.8) + 32
        fahr_list.append(fahrx)   
    print("Wind Chill temperatures")
    print("Celsius", "%13s" % "Fahrenheit", "%8s" % "5mph" "%12s" % "10mph" "%12s" % "15mph" "%12s" % "20mph" "%12s" % "25mph" "%12s" % "30mph" "%12s" % "35mph" "%10s" % "40mph")
    zipped = zip(xy_list, fahr_list)
    for xy_list, fahr_list, Twc_list5, Twc_list10, Twc_list15, Twc_list20, Twc_list25, Twc_list30, Twc_list35, Twc_list40 in zip(xy_list, fahr_list, Twc_list5, Twc_list10, Twc_list15, Twc_list20, Twc_list25, Twc_list30, Twc_list35, Twc_list40):
        print(f'{xy_list}' "%17s" %   f'{fahr_list}' "%14s" % f'{Twc_list5}' "%12s" % f'{Twc_list10}' "%12s" % f'{Twc_list15}' "%12s" % f'{Twc_list20}' "%12s" % f'{Twc_list25}' "%12s" % f'{Twc_list30}' "%12s" % f'{Twc_list35}' "%8s" % f'{Twc_list40}')
    
   

    for xy_list, fahr_list, Hi_list40 in zip(xy_list, fahr_list, Hi_list40):
        print(f'{xy_list}' "%17s" % f'{fahr_list}' "%14s" % f'{Hi_list40}')
this file does all the math and I import this in my main function:
def compute_wind_chill(x, y):
    Twc_list5 = []
    Twc_list10 = []
    Twc_list15 = []
    Twc_list20 = []
    Twc_list25 = []
    Twc_list30 = []
    Twc_list35 = []
    Twc_list40 = []
    fahr_list = []
    for i in range(x, y):
        fahr_val = (i * 1.8) + 32
        fahr_list.append(fahr_val)
    for i in fahr_list:
        if i > 50:
            Twc_list5.append("X")
        else:
            Twc5 = 35.74 + (0.6215 * i) - (35.75 * (5 ** 0.16)) + (0.4275 * i * (5 ** 0.16))
            Twc_list5.append("{:,.2f}".format(Twc5))
    for i in fahr_list:
        if i > 50:
            Twc_list10.append("X")
        else:
            Twc10 = 35.74 + (0.6215 * i) - (35.75 * (10 ** 0.16)) + (0.4275 * i * (10 ** 0.16))
            Twc_list10.append("{:,.2f}".format(Twc10))
    for i in fahr_list:
        if i > 50:
            Twc_list15.append("X")
        else:
            Twc15 = 35.74 + (0.6215 * i) - (35.75 * (15 ** 0.16)) + (0.4275 * i * (15 ** 0.16))
            Twc_list15.append("{:,.2f}".format(Twc15))
    for i in fahr_list:
        if i > 50:
            Twc_list20.append("X")
        else:
            Twc20 = 35.74 + (0.6215 * i) - (35.75 * (20 ** 0.16)) + (0.4275 * i * (20 ** 0.16))
            Twc_list20.append("{:,.2f}".format(Twc20))
    for i in fahr_list:
        if i > 50:
            Twc_list25.append("X")
        else:
            Twc25 = 35.74 + (0.6215 * i) - (35.75 * (25 ** 0.16)) + (0.4275 * i * (25 ** 0.16))
            Twc_list25.append("{:,.2f}".format(Twc25))
    for i in fahr_list:
        if i > 50:
            Twc_list30.append("X")
        else:
            Twc30 = 35.74 + (0.6215 * i) - (35.75 * (30 ** 0.16)) + (0.4275 * i * (30 ** 0.16))
            Twc_list30.append("{:,.2f}".format(Twc30))
    for i in fahr_list:
        if i > 50:
            Twc_list35.append("X")
        else:
            Twc35 = 35.74 + (0.6215 * i) - (35.75 * (35 ** 0.16)) + (0.4275 * i * (35 ** 0.16))
            Twc_list35.append("{:,.2f}".format(Twc35))
    for i in fahr_list:
        if i > 50:
            Twc_list40.append("X")
        else:
            Twc40 = 35.74 + (0.6215 * i) - (35.75 * (40 ** 0.16)) + (0.4275 * i * (40 ** 0.16))
            Twc_list40.append("{:,.2f}".format(Twc40))
    
    
    
    return(Twc_list5, Twc_list10, Twc_list15, Twc_list20, Twc_list25, Twc_list30, Twc_list35, Twc_list40)

def compute_heat_index(x, y):
    Hi_list40 = []
    Hi_list50 = []
    Hi_list60 = []
    Hi_list70 = []
    Hi_list80 = []
    Hi_list90 = []
    Hi_list100 = []
    fahr_list = []
    for i in range(x, y):
        fahr_val = (i * 1.8) + 32
        fahr_list.append(fahr_val)
    for i in fahr_list:
        if i < 80:
            Hi_list40.append("X")
        else:
            Hi40 = -42.379 + (2.04901523 * i) + (10.14333127 * .4) + (-.22475541 * i * .4) + (-6.83783 * 10 ** -3 * (i ** 2)) + (-5.481717 * 10 ** -2 * (.4 ** 2)) + (1.22874 * 10 ** -3 * (i ** 2) * .4) + (8.5282 * 10 ** -4 * i * (.4 ** 2)) + (-1.99 * 10 ** -6 * (i ** 2) * (.4 ** 2))
            Hi_list40.append(Hi40)
            
    return(Hi_list40)
Reply
#2
Hello,
Please show the Traceback. It contains useful information.
I speak Python but I don't speak English (I just read it a little). If I express myself badly, please blame the translator^^.
Reply
#3
If you want formatted strings, you need to specify a format. When printing windchill you specify 2 decimal places, but you don't do that for fahrenheit. At least I don't think you do. It is difficult to say because your formatting is messy. If you want your columns to line up you should also specify a width in the format.

This is really hard to read.
print("Celsius", "%13s" % "Fahrenheit", "%8s" % "5mph" "%12s" % "10mph" "%12s" % "15mph" "%12s" % "20mph" "%12s" % "25mph" "%12s" % "30mph" "%12s" % "35mph" "%10s" % "40mph")
You can clean it up using the format() command.
windspeeds = (5, 10, 15, 20, 25, 30, 35, 40)
print("Celsius {:>13} {:3} mph {:3} mph {:3} mph {:3} mph {:3} mph {:3} mph {:3} mph {:3} mph".format("Fahrenheit", *windspeeds))
Or better yet use a str.join().
windspeeds = (5, 10, 15, 20, 25, 30, 35, 40)
print("Celsius   Fahernheit", " ".join([f"{ws:3} mph" for ws in windspeeds]))
I like this last way because you can change the windspeed table by just adding or subtracting values from the windspeeds list.

Breaking large code into smaller chunks is a weak reason for writing a function. A better reason for writing a function is to create a code block that can be reused. Not only does code reuse often reduce the amount of code, reusing code results in less debugging and testing. Your functions are of the first type. They have only one purpose and are only called once. They also have a lot of duplicate code. Any changes to the windspeed calculation has to be applied to each wind speed. There are also lots of opportunities for cut and paste errors.

I would write wind chill and heat index as reusable functions. My wind chill function would take two arguments, temperature and wind speed, and return a temperature. This one function would be reused many times resulting in a substantial reduction in the amount of code. I would also return the temperature as a number, not a string, making my wind chill function useful for things other than one use.

And since I'm not taking your class, I would use the tabulate library to print the table. The format suggestions above should help you make a nice looking table without tabulate.
from tabulate import tabulate

windspeeds = (5, 10, 15, 20, 25, 30, 35, 40)

def windchill(deg, ws):
    if deg > 50:
        return deg
    return 35.74 + (0.6215 * deg) - (35.75 * (ws ** 0.16)) + (0.4275 * deg * (ws ** 0.16))

start, stop, *step = map(int, input("Input Temperature range in degc: ").split())
step = step[0] if len(step) > 0 else 1

# Make a table for the tabulate function
data = [["Celsius", "Fahrenheit"] + [f"{ws:>2} mph" for ws in windspeeds]]
for degc in range(start, stop+1, step):
    degf = degc * 1.8 + 32
    data.append([degc, f"{degf:.2f}"] + [f"{windchill(degf, ws):.2f}" if degf <= 50 else "X" for ws in windspeeds])

print(tabulate(data, headers="firstrow", tablefmt="fancy_grid"))
Output:
Input Temperature range in degc: -20 20 5 ╒═══════════╤══════════════╤══════════╤══════════╤══════════╤══════════╤══════════╤══════════╤══════════╤══════════╕ │ Celsius │ Fahrenheit │ 5 mph │ 10 mph │ 15 mph │ 20 mph │ 25 mph │ 30 mph │ 35 mph │ 40 mph │ ╞═══════════╪══════════════╪══════════╪══════════╪══════════╪══════════╪══════════╪══════════╪══════════╪══════════╡ │ -20 │ -4 │ -15.21 │ -20.89 │ -24.52 │ -27.24 │ -29.44 │ -31.30 │ -32.91 │ -34.34 │ ├───────────┼──────────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┤ │ -15 │ 5 │ -4.64 │ -9.74 │ -12.99 │ -15.44 │ -17.41 │ -19.07 │ -20.52 │ -21.80 │ ├───────────┼──────────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┤ │ -10 │ 14 │ 5.93 │ 1.42 │ -1.47 │ -3.63 │ -5.38 │ -6.85 │ -8.13 │ -9.27 │ ├───────────┼──────────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┤ │ -5 │ 23 │ 16.50 │ 12.57 │ 10.06 │ 8.18 │ 6.66 │ 5.37 │ 4.26 │ 3.27 │ ├───────────┼──────────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┤ │ 0 │ 32 │ 27.08 │ 23.73 │ 21.59 │ 19.99 │ 18.69 │ 17.60 │ 16.65 │ 15.81 │ ├───────────┼──────────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┤ │ 5 │ 41 │ 37.65 │ 34.88 │ 33.12 │ 31.79 │ 30.72 │ 29.82 │ 29.04 │ 28.34 │ ├───────────┼──────────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┤ │ 10 │ 50 │ 48.22 │ 46.04 │ 44.64 │ 43.60 │ 42.76 │ 42.04 │ 41.43 │ 40.88 │ ├───────────┼──────────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┤ │ 15 │ 59 │ X │ X │ X │ X │ X │ X │ X │ X │ ├───────────┼──────────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┤ │ 20 │ 68 │ X │ X │ X │ X │ X │ X │ X │ X │
To answer the error question.

When you do this:
   for xy_list, fahr_list, Twc_list5, Twc_list10, Twc_list15, Twc_list20, Twc_list25, Twc_list30, Twc_list35, Twc_list40 in zip(xy_list, fahr_list, Twc_list5, Twc_list10, Twc_list15, Twc_list20, Twc_list25, Twc_list30, Twc_list35, Twc_list40):
        print(f'{xy_list}' "%17s" %   f'{fahr_list}' "%14s" % f'{Twc_list5}' "%12s" % f'{Twc_list10}' "%12s" % f'{Twc_list15}' "%12s" % f'{Twc_list20}' "%12s" % f'{Twc_list25}' "%12s" % f'{Twc_list30}' "%12s" % f'{Twc_list35}' "%8s" % f'{Twc_list40}')
You set xy_list to an int and fahr_list to a float. Now when you try to use them as lists your program crashes.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020