要更改类型变量,例如a,仅执行float (a) 是不够的,因为您必须分配此a = float(a),因为您多次使用height 和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): "))
避免以下行中的所有错误
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
========================