首页 > 解决方案 > 为什么我的数据在使用拆分功能时没有转换为浮点类型。#python3

问题描述

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)

标签: pythonfunctionsplit

解决方案


您在第一行中所做的是获取整个字符串,例如“3.5,5.6,9”,并尝试将其转换为浮点数,然后将其分开。问题是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 ;)

对第二行应用相同的逻辑


推荐阅读