Python 我得到TypeError:can';t将序列乘以类型为'的非整数;浮动';
我收到了TypeError:在运行代码时,无法将序列与“float”类型的非int相乘。有人能给我解释一下问题是什么吗?基本上,我正在做一个用于家庭作业的孔径计算器Python 我得到TypeError:can';t将序列乘以类型为'的非整数;浮动';,python,python-2.7,Python,Python 2.7,我收到了TypeError:在运行代码时,无法将序列与“float”类型的非int相乘。有人能给我解释一下问题是什么吗?基本上,我正在做一个用于家庭作业的孔径计算器 import time as t #imports time so the code will look much better. import random #imports random import math #imports math pih = () inputs = [] #Brackets to make it a
import time as t #imports time so the code will look much better.
import random #imports random
import math #imports math
pih = ()
inputs = [] #Brackets to make it a list
#All the functions
def x_round(x): #A function that rounds the thing inside the bracket the next 0.25w
return math.ceil(x*4)/4 #whenever x_round something, goes to nearest 0.25
def isFloat(x):
try:
float(x)
inputs.append("float")
float(x)
except ValueError:
inputs.append("why")
print("Bore Size Caculator")
print("========================")
while 1 == 1:
choice = input("Would you like to determine the bore-size (B) or the volume (V) of the tunnel? ")
pi = 3.14159265358979 #Gets variable pi as the number pi
choice=choice.lower()
if choice == "b":
volume=input("What is the minimum volume that is required? (cubic metres): ")
height=input("How long do you need the tunnel to be? (metres): ")
isFloat(volume)
isFloat(height)
isfloat=inputs[-1]
if isfloat == "float":
float(height)
float(volume)
print(pi)
print(height)
pih = pi * height
vpih = volume / pih
radius = math.sqrt(vpih)
roundedradius = x_round(radius)
if roundedradius > 8:
increased=volume/(pi*(64))
increased=round(increased , 2)
print("There is no bore size big enough, increase the legnth to",increased)
elif roundedradius <= 8:
print("Bore radius size required:",roundedradius)
exactvolume = round(pih *( roundedradius * roundedradius ), 2)
print("Exact volume produced (with bore-size above and the tunnel legnth specified)",exactvolume)
else:
print("Please input a valid number")
print("========================")
导入时间,因为t#导入时间,所以代码看起来会更好。
导入随机#导入随机
导入数学#导入数学
pih=()
inputs=[]#括号将其列为一个列表
#所有功能
def x#u round(x):#一个函数,它将括号内的对象旋转0.25w
返回math.ceil(x*4)/4#每当x#u取整某物时,都精确到0.25
def isFloat(x):
尝试:
浮动(x)
输入。追加(“浮动”)
浮动(x)
除值错误外:
inputs.append(“为什么”)
打印(“孔径计算器”)
打印(“=====================================”)
而1==1:
选择=输入(“您想确定隧道的钻孔尺寸(B)还是体积(V?”)
pi=3.14159265358979#获取变量pi作为数字pi
choice=choice.lower()
如果选项==“b”:
体积=输入(“所需的最小体积是多少?(立方米):”)
高度=输入(“隧道需要多长?(米):”)
isFloat(体积)
isFloat(高度)
isfloat=输入[-1]
如果isfloat==“float”:
浮子(高度)
浮动(体积)
打印(pi)
打印(高度)
pih=pi*高度
vpih=体积/pih
半径=数学sqrt(vpih)
圆形半径=x_圆形(半径)
如果圆形半径>8:
增加=体积/(pi*(64))
增加=轮数(增加,2)
打印(“没有足够大的孔尺寸,将长度增加到”,增加)
elif roundedradius要更改类型的变量,例如a
,仅执行float(a)
,是不够的,因为您必须指定此a=float(a)
,因为您使用高度和体积时,我多次将其转换为float,如下所示:
volume=float(input("What is the minimum volume that is required? (cubic metres): "))
height=float(input("How long do you need the tunnel to be? (metres): "))
避免以下行中的所有错误
import time as t #imports time so the code will look much better.
import random #imports random
import math #imports math
pih = ()
inputs = [] #Brackets to make it a list
#All the functions
def x_round(x): #A function that rounds the thing inside the bracket the next 0.25w
return math.ceil(x*4)/4 #whenever x_round something, goes to nearest 0.25
def isFloat(x):
try:
float(x)
inputs.append("float")
float(x)
except ValueError:
inputs.append("why")
print("Bore Size Caculator")
print("========================")
while 1 == 1:
choice = input("Would you like to determine the bore-size (B) or the volume (V) of the tunnel? ")
pi = 3.14159265358979 #Gets variable pi as the number pi
choice=choice.lower()
if choice == "b":
volume=float(input("What is the minimum volume that is required? (cubic metres): "))
height=float(input("How long do you need the tunnel to be? (metres): "))
isFloat(volume)
isFloat(height)
isfloat=inputs[-1]
if isfloat == "float":
print(pi)
print(height)
pih = pi * height
vpih = volume / pih
radius = math.sqrt(vpih)
roundedradius = x_round(radius)
if roundedradius > 8:
increased=volume/(pi*(64))
increased=round(increased , 2)
print("There is no bore size big enough, increase the legnth to",increased)
elif roundedradius <= 8:
print("Bore radius size required:",roundedradius)
exactvolume = round(pih *( roundedradius * roundedradius ), 2)
print("Exact volume produced (with bore-size above and the tunnel legnth specified)",exactvolume)
else:
print("Please input a valid number")
print("========================")
是否有办法防止回溯(最近一次调用最后一次):“File”program.py,第31行,volume=float(输入(“所需的最小体积是多少?(立方米):”)ValueError:如果用户输入的内容不是数字,则无法将字符串转换为浮点:“agsfjhgasf”?我已使用此改进更新了代码。看看代码。嗨,欢迎来到堆栈溢出。虽然您已经收到了一个答案,希望是一个有帮助的答案,但请注意(对于将来的问题),准确地说出错误的来源(它在回溯中显示),以及如何提供一个仍然再现错误的最小示例(作为旁注,这也是调试过程中有用的一部分)是很重要的。要了解更多信息,请访问:。祝你好运和快乐!
import time as t #imports time so the code will look much better.
import random #imports random
import math #imports math
pih = ()
inputs = [] #Brackets to make it a list
#All the functions
def x_round(x): #A function that rounds the thing inside the bracket the next 0.25w
return math.ceil(x*4)/4 #whenever x_round something, goes to nearest 0.25
def isFloat(x):
try:
float(x)
inputs.append("float")
float(x)
except ValueError:
inputs.append("why")
print("Bore Size Caculator")
print("========================")
choice=0
while choice !='q':
choice = input("Would you like to determine the bore-size (B) or the volume (V) of the tunnel or exit (Q)? ")
pi = 3.14159265358979 #Gets variable pi as the number pi
choice=choice.lower()
if choice == "b":
enter=False
while enter==False:
try:
volume=float(input("What is the minimum volume that is required? (cubic metres): "))
height=float(input("How long do you need the tunnel to be? (metres): "))
enter=True
print(pi)
print(height)
pih = pi * height
vpih = volume / pih
radius = math.sqrt(vpih)
roundedradius = x_round(radius)
if roundedradius > 8:
increased=volume/(pi*(64))
increased=round(increased , 2)
print("There is no bore size big enough, increase the legnth to",increased)
elif roundedradius <= 8:
print("Bore radius size required:",roundedradius)
exactvolume = round(pih *( roundedradius * roundedradius ), 2)
print("Exact volume produced (with bore-size above and the tunnel legnth specified)",exactvolume)
except:
print("Please input a valid number")
print("========================")
Bore Size Caculator
========================
Would you like to determine the bore-size (B) or the volume (V) of the tunnel or exist (Q)? B
What is the minimum volume that is required? (cubic metres): 20
How long do you need the tunnel to be? (metres): 1000
3.14159265358979
1000.0
Bore radius size required: 0.25
Exact volume produced (with bore-size above and the tunnel legnth specified) 196.35
========================
Would you like to determine the bore-size (B) or the volume (V) of the tunnel or exist (Q)? B
What is the minimum volume that is required? (cubic metres): ABBDBDBBBF
Please input a valid number
What is the minimum volume that is required? (cubic metres): 12
How long do you need the tunnel to be? (metres): 200
3.14159265358979
200.0
Bore radius size required: 0.25
Exact volume produced (with bore-size above and the tunnel legnth specified) 39.27
========================
Would you like to determine the bore-size (B) or the volume (V) of the tunnel or exist (Q)? Q
========================