为什么我的数据在使用split函数时没有转换为float类型#Python3

2024-10-02 20:30:10 发布

您现在位置:Python中文网/ 问答频道 /正文

u,p,k = float(input(" enter the values of viscoity, pressure , prmeability")).split(",")

l,A = int(input("enter the vaues of length and area").split(" "))


def flow_rate(u,p,k,l,A):

  Q=k* A * p/l*u

  print(f"the Q is {Q}")
flow_rate(u,p,k,l,A)

Tags: oftheinputratefloatflowlengthint
2条回答

在第一行中,你要做的是取整个字符串,比如“3.5,5.6,9”,试着把它转换成float,然后把它分开。问题是python无法将包含3个数字和逗号的字符串转换为浮点。解决此问题的方法是首先拆分字符串,然后将每个元素转换为float,如下所示:

u, p, k = map(float, input("Enter the values of viscosity, pressure, and permeability: ").split(", ")
# I changed the split separator so the input can be prettier ;)

对第二行应用相同的逻辑

这是因为您不能在列表上调用int,而希望将值转换为int

input()         -> str       : "1 2 3"
input().split() -> list[str] : ["1", "2", "3"]

使用map

l,A = map(int, input("enter the vaues of length and area").split())

或者一份清单

l, A = [int(x) for x in input("enter the vaues of length and area").split()]

相关问题 更多 >