首页 > 解决方案 > 我得到 TypeError: can't multiply sequence by non-int of type 'float'

问题描述

我收到 TypeError: can't multiply sequence by non-int of type '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("========================")

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

标签: pythonpython-2.7

解决方案


例如,要更改类型的变量a,这样做是不够的,float (a)因为您必须在使用时分配 this a = float(a)height并且volume有几次我已经像这样转换为 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("========================")

我还认为您应该设置 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("========================")
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
========================

推荐阅读