【问题标题】:Error: "can't multiply sequence by non-int of type 'float'"错误:“不能将序列乘以‘float’类型的非整数”
【发布时间】:2017-10-06 15:41:20
【问题描述】:

它说错误是当 h(高度)在 11000 和 25000 之间时,所以我只在我的所有 if 循环和发生问题的特定循环之外发布了初始内容。这是我的代码:

import math;
T = 0.0;
P = 0.0;
hString = ("What is the altitude in meters?");
h = int(hString);
e = math.exp(0.000157*h);

elif 11000 < h < 25000:
   T = -56.46;
   P = (22.65)*[(1.73)-e];

【问题讨论】:

  • 你的代码给了我ValueError: invalid literal for int() with base 10: 'What is the altitude in meters?'。您能否更正您的代码示例,使其在复制粘贴到 Python 控制台时运行?
  • 请发布实际产生您报告的错误的代码。这个关于写MVCE 的页面可能会有所帮助。

标签: python-3.x math


【解决方案1】:

当你使用数学运算时,你需要小心使用括号。

    P = (22.65)*((1.73)-e); #will be right way of using

[ ] 使用将创建一个您在此程序中不需要的列表。

这是一个link,它将帮助您了解更多关于类型转换和正确使用括号的知识。

在你的代码中你也没有使用过

      hString =input ("What is the altitude in meters?");
      h = int(hString);

输入将允许您从用户那里获取价值,然后int(your_input) 将帮助您转换为整数

【讨论】:

  • P = (22.65)*)(1.73)-e) 在我看来不合适。您的意思是:P = (22.65)*((1.73)-e)
【解决方案2】:

最后一行中的方括号 ([(1.73)-e]) 创建一个列表。在这种情况下,它是一个包含一个元素的列表,即(1.73)-e。我想你打算把那些当成父母。进行更改,它将起作用。

最后一行变成:

P = (22.65)*((1.73)-e);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2010-12-30
    • 2012-10-09
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多