【问题标题】:Unsupported format string passed to NoneType.__format_传递给 NoneType.__format_ 的格式字符串不受支持
【发布时间】:2021-10-27 20:41:38
【问题描述】:

我有一个程序可以计算特定公式的测量值。结果应显示在控制台和一排解决方案上。不幸的是,当我运行这个程序时,出现了错误。我曾尝试使用zip(),但没有帮助。

这是回溯错误:

  File "W:/Labaratorni/micro/main.py", line 34, in main
print(f'T[C] = {temperature - 273.15:.2f} \t R = {resistance:.3f}')
TypeError: unsupported format string passed to NoneType.__format__

这是我的代码:

import numpy as np

K = 273.15
T = [1.4, 25, 72]
R = [3440, 1175, 453]


def make_solve(temperature_kalvin: float, temperatures: list, resistances: list):
    matrix_a = []
   for element in R:
        matrix_a.append([1, np.log(element), pow(np.log(element), 3)])
    matrix_b = np.array([1 / (t + K) for t in T])
    solve = np.linalg.solve(np.array(matrix_a), matrix_b)
    return solve


def resistance_from_temperature(temperature: float, parameter_a: float, parameter_b: float, 
parameter_c: float):
    x = 1 / parameter_c * (parameter_a - 1 / temperature)
    y = pow((pow((parameter_b / (3 * parameter_c)), 3) + pow(x / 2, 2)), .5)
    result = np.exp((y - x / 2) ** (1 / 3) - (y + x / 2) ** (1 / 3)) / 7.3


def get_list_of_second_parameters():
    solves = make_solve(K, T, R)
    temperatures = np.linspace(0 + K, 100 + K, 101)
    resistances = [resistance_from_temperature(temperature, solves[0], solves[1], solves[2]) for temperature in temperatures]
return temperatures, resistances


def main():
     temperatures, resistances = get_list_of_second_parameters()
     for temperature, resistance in zip(temperatures, resistances):
         print(f'T[C] = {temperature - 273.15:.2f} \t R = {resistance:.3f}')
     input()
main()

【问题讨论】:

  • resistance_from_temperature 没有return,这意味着它隐式返回None。然后你将 None 传播到你的 f-string 中并尝试 format 它就好像它是一个字符串
  • @G.Anderson?谢谢,帮了大忙

标签: python python-3.x numpy


【解决方案1】:

在这种情况下,阻力是回南,但问题出在这条线上

y = pow((pow((parameter_b / (3 * parameter_c)), 3) + pow(x / 2, 2)), .5)

它没有计算 y 的值。

另一个问题 resistance_from_temperature 没有返回结果值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2017-12-30
    • 2021-10-31
    相关资源
    最近更新 更多