我得到TypeError:不能将序列乘以“float”类型的nonit

2024-04-16 16:47:07 发布

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

我收到了TypeError:在运行代码时,不能将sequence与类型为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 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("========================")

有人能告诉我我做错了什么吗?谢谢


Tags: thetoinputsizepimathfloatinputs
1条回答
网友
1楼 · 发布于 2024-04-16 16:47:07

要更改类型为a的变量,只做float (a)是不够的,因为您必须分配这个a = float(a),因为您多次使用heightvolume我已将其转换为浮点数,如下所示:

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

Output:

 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
========================

相关问题 更多 >