Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Typeerror
#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


Messages In This Thread
Typeerror - by Leo - Mar-28-2022, 12:24 AM
RE: Typeerror - by Coricoco_fr - Mar-28-2022, 06:24 AM
RE: Typeerror - by deanhystad - Mar-28-2022, 06:26 AM

Forum Jump:

User Panel Messages

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