我一直收到这个错误:
Traceback (most recent call last):
File "C:\Users\Andrew\Desktop\lab10.py", line 66, in <module>
main()
File "C:\Users\Andrew\Desktop\lab10.py", line 55, in main
volumeRectangle=VR(length,width,height)
File "C:\Users\Andrew\Desktop\lab10.py", line 20, in VR
volume=length*width*height
TypeError: can't multiply sequence by non-int of type 'function'
代码
import math
def AC(radius):
area = math.pi * radius ** 2
return area
def AR(length,width):
area=length*width
return area
def VC(radius,height):
volume=math.pi*radius*radius*height
return volume
def VR(length,width,height):
volume=length*width*height
return volume
# WRITE ALL THE OTHER FUNCTIONS
def main():
inFile = open("lab10.input","r")
# get calculation type, but wait on dimension(s)
type = (inFile.readline()).strip()
while (type != "###"):
if (type == "AC"):
radius = eval(inFile.readline())
circleArea = AC(radius)
print(format("Area of a Circle","30s"),format(circleArea,"15.2f"))
if (type=='AR'):
length=eval(inFile.readline())
width=eval(inFile.readline())
rsArea=ARR(length,width)
print(format("Area of a Rectangle or Square",'30s'),format(rsArea,'15.2f'))
if (type=='VC'):
radius=eval(inFile.readline())
height=eval(inFile.readline())
volumeCylinder=VC(radius,height)
print(format("Volume of a Cylinder",'30s'),format(volumeCylinder,'15.2f'))
if (type=='VR'):
length=eval(inFile.readline())
width=eval(inFile.readline())
height=eval(inFile.readline())
volumeRectangle=VR(length,width,height)
print(format("Volume of a Rectangle",'30s'),format(volumeRectangle,'15.2f'))
# do the processing for all other types of calculations
# get calculation type, but wait on dimension(s)
type = (inFile.readline()).strip()
main()
这是输入文件的样子。输入文件
AC
7.5
SAC
4
VR
2, 3, 4.1
AR
13, 3.25
SAS
24
###
0
答案 0 :(得分:2)
这似乎有效。就像保罗说的那样,同样的输入也搞砸了。
while (type != "###"):
if (type == "AC"):
radius = eval(inFile.readline())
circleArea = AC(radius)
print(format("Area of a Circle","30s"),format(circleArea,"15.2f"))
if (type=='AR'):
length, width=eval(inFile.readline().strip())
rsArea=AR(length,width)
print(format("Area of a Rectangle or Square",'30s'),format(rsArea,'15.2f'))
if (type=='VC'):
radius, height=eval(inFile.readline().strip())
volumeCylinder=VC(radius,height)
print(format("Volume of a Cylinder",'30s'),format(volumeCylinder,'15.2f'))
if (type=='VR'):
length, width, height =eval(inFile.readline().strip())
volumeRectangle=VR(length,width,height)
print(format("Volume of a Rectangle",'30s'),format(volumeRectangle,'15.2f'))
答案 1 :(得分:0)
点击VR
命令时遇到问题。这是因为它有多个输入参数。你已经侥幸逃脱了它。在第一个命令(AC
)中,因为它只有一个参数而eval
将其转换为单个float
值。
对于VR
,有3个参数,您试图通过读取文件中的一行(即读取的3行)来设置每个参数。现在你的所有参数实际上都在一行上。所以,如果你认为你正在阅读参数
12, 3, 4.1
你不是在读书
2, 3, 4.1
AR
13, 3.25
当你eval
这些你得到第1行和第3行的元组,并且因为AR
是你定义的函数,eval('AR')
会返回你的那个函数(这意味着异常错误)你收到的消息)。这是使用eval
的危险之一,它可能会导致意外结果。
所以你的VR电话看起来像是
VR((2, 3, 4.1), AR, (13, 3.25))
这无疑不是你所期望的。
您可以通过两种方式解决它。为了清晰起见,我缩写了您的代码。你显然需要在其他命令中复制。
坚持tuples
和ast.literal_eval(使用eval
而不是import math
from ast import literal_eval
def VR(length,width,height):
return length * width * height
def main():
with open("lab10.input","r") as inFile:
acttype = (inFile.readline()).strip()
while (acttype != "###"):
if (acttype=='VR'):
params = literal_eval(inFile.readline())
volumeRectangle = VR(*params)
print(format("Volume of a Rectangle",'30s'),format(volumeRectangle,'15.2f'))
acttype = (inFile.readline()).strip()
main()
)。
eval
或通过在,
上拆分行来避开import math
def VR(length,width,height):
return length * width * height
def main():
with open("lab10.input","r") as inFile:
acttype = (inFile.readline()).strip()
while (acttype != "###"):
if (acttype=='VR'):
params = map(float, inFile.readline().split(','))
volumeRectangle = VR(*params)
print(format("Volume of a Rectangle",'30s'),format(volumeRectangle,'15.2f'))
acttype = (inFile.readline()).strip()
main()
。
Route::filter('no.session.cookie', function()
{
Config::set('session.driver', 'array');
Config::set('cookie.driver', 'array');
});