Python新手求助:TypeError: unsupported operand type(s) for *: 'function' and 'int' 错误修复
Hey there! As someone who's only been learning Python for two weeks, you're already tackling some neat atmospheric calculations—great start! Let's break down why this error is popping up and get your code working properly.
What's Causing the Error?
When you call MR(p_w, p_tot), you're passing the function objects p_w and p_tot directly, not the numerical values those functions compute for each altitude h. Python can't multiply a float (like your constant B) with a function, which is exactly what that TypeError is complaining about.
You need to first calculate the actual values of p_w and p_tot for every height in your h array, then pass those computed values to MR.
Step-by-Step Fix
All your variables depend on h in a clear chain:h → t(h) / rh(h) / p_tot(h) → p_ws(t) → p_w(p_ws, rh) → MR(p_w, p_tot)
Since h is a NumPy array, we can use vectorized calculations (no messy loops needed!) to compute all values at once. Here's the revised code:
import numpy as np import matplotlib.pyplot as plt def MR(p_w, p_tot): # Quick check: The standard air-water mass ratio formula is (B * p_w) / (p_tot - p_w) # I kept your original formula, but double-check if this matches your requirements! return (B * p_w / p_tot * p_w) def p_w(p_ws, rh): return p_ws * (rh / 100) def p_ws(t): # Fixed a likely typo here: original had (m*t)/t + t_n which simplifies to m + t_n # This uses the standard form for saturation vapor pressure: (m*t)/(t + t_n) return A * 10 ** ((m * t) / (t + t_n)) def rh(h): return -0.0041 * h + 60.1941 def t(h): return 293.15 - 0.0065 * h def p_tot(h): return p_b * (1 + (beta / t_0) * h) ** -(g / (beta * R)) # Define all constants upfront A = 6.116441 m = 7.591386 t_n = 240.7263 B = 621.9907 p_b = 101325 beta = 1.458 * 10**(-6) t_0 = 293.15 g = 9.81 R = 287.05287 # Generate your altitude array h = np.array(range(5001)) # Calculate each variable step-by-step for all heights t_vals = t(h) rh_vals = rh(h) p_ws_vals = p_ws(t_vals) p_w_vals = p_w(p_ws_vals, rh_vals) p_tot_vals = p_tot(h) # Now compute MR using the actual numerical values mr_vals = MR(p_w_vals, p_tot_vals) # Plot the results plt.plot(h, mr_vals) plt.xlabel('Altitude (m)') plt.ylabel('Mass Ratio water in air (g/kg)') plt.grid() plt.show()
Key Things to Verify
- Typo in
p_wsFormula: Your originalp_wsfunction had(m*t)/t + t_n—this would cancel outtand just givem + t_n, which doesn't make sense for saturation vapor pressure. I adjusted it to match the standard Tetens' formula variant you're using. If your original formula was intentional, feel free to revert this! - MR Formula Accuracy: The standard air-water mass ratio uses
(B * p_w) / (p_tot - p_w)(subtracting water vapor pressure from total pressure in the denominator). Your current formula is(B*p_w/p_tot*p_w)—make sure this is the correct equation for your academic work!
This should get your code running and plotting the MR values against altitude as expected. Happy coding!
内容的提问来源于stack exchange,提问作者TbjK

