我收到TypeError:无法将序列乘以'float'类型的非整数

时间:2019-08-15 04:05:32

标签: python python-2.7

我收到TypeError:运行代码时,无法将序列乘以'float'类型的非整数。有人可以向我解释什么问题吗?基本上,我是在做作业的孔尺寸计算器

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("========================")

那么谁能告诉我我做错了什么?谢谢

1 个答案:

答案 0 :(得分:1)

要更改类型为a的变量,这样做float (a)是不够的,因为在使用a = float(a)和{时,您必须分配此height {1}}多次转换为这样的float:

volume

避免以下各行中的所有错误

   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): "))

我还认为您应该放置while循环的退出条件,然后为您展示改进的版本和输出:

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("========================")
  

输出:

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("========================")