无法遵循嵌套for循环的行为

时间:2016-05-11 11:29:00

标签: python sqlite

我使用的是Python3.5并且有一个SQLite数据库,其中填充了我正在选择并执行计算的数据。我正在使用的代码如下。

def Horizontal_Cyl ():
c.execute("SELECT DIR_Dish1 FROM Tanks")  
DIR_Dish1 = c.fetchall()  
print ("Dish ",DIR_Dish1)  
c.execute("SELECT DIR_Radius FROM Tanks")
DIR_Radius = c.fetchall()
print ("Radius",DIR_Radius)
c.execute("SELECT VAR_LevelReg FROM Tanks")
VAR_LevelReg = c.fetchall()
print ("Level",VAR_LevelReg)
c.execute("SELECT DIR_Length FROM Tanks")
DIR_Length = c.fetchall()
print ("Length",DIR_Length)
x = Dish_Vol()  
dish1Volume = x 
for row in c.execute("SELECT DIR_Radius, VAR_LevelReg, DIR_Length FROM Tanks"):
    for DIR_Radius in row:
        for VAR_LevelReg in row:
            for DIR_Length in row:
                cylinderVolume = ((( DIR_Radius * DIR_Radius) * math.acos((DIR_Radius - VAR_LevelReg ) / DIR_Radius)) - \
                ((DIR_Radius - VAR_LevelReg) * math.sqrt((DIR_Radius * DIR_Radius) - \
                ((DIR_Radius - VAR_LevelReg) * (DIR_Radius - VAR_LevelReg))))) * (DIR_Length / 1000)

                volume = cylinderVolume / 1000

                Total = volume + dish1Volume + dish1Volume
                print ("Total: ",Total)

Dish_Vol是另一段代码,它给我一个数字x,以便以后使用。

我遇到麻烦的地方就在这里:

for row in c.execute("SELECT DIR_Radius, VAR_LevelReg, DIR_Length FROM Tanks"):
    for DIR_Radius in row:
        for VAR_LevelReg in row:
            for DIR_Length in row:
                cylinderVolume = ((( DIR_Radius * DIR_Radius) * math.acos((DIR_Radius - VAR_LevelReg ) / DIR_Radius)) - \
                ((DIR_Radius - VAR_LevelReg) * math.sqrt((DIR_Radius * DIR_Radius) - \
                ((DIR_Radius - VAR_LevelReg) * (DIR_Radius - VAR_LevelReg))))) * (DIR_Length / 1000)

                volume = cylinderVolume / 1000

                Total = volume + dish1Volume + dish1Volume
                print ("Total: ",Total)

对于Total,我得到了输出:

Total:  4664.471999862134
Total:  5045.348779887742
Total:  27614.512396288894
Total:  5148.702861581893
Total:  5575.141845226646
Total:  30844.130922594857
Traceback (most recent call last):
  File "C:\Users\TJgro\workspace\SQLite Practise\Dish_Vol\Calculations    \__init__.py", line 204, in <module>
main()
  File "C:\Users\TJgro\workspace\SQLite Practise\Dish_Vol\Calculations\__init__.py", line 202, in main
Horizontal_Cyl ()
  File "C:\Users\TJgro\workspace\SQLite Practise\Dish_Vol\Calculations\__init__.py", line 190, in Horizontal_Cyl
cylinderVolume = ((( DIR_Radius * DIR_Radius) * math.acos((DIR_Radius - VAR_LevelReg ) / DIR_Radius)) - \
ValueError: math domain error

错误前列出的最终总数; 30,844 ....是我正在寻找的正确结果。我假设的数学域错误是由于某些地方在执行与三角函数交互时迭代的负输出。

我似乎无法理解的是,for循环是如何表现的,我似乎无法跟随它们并得出作为输出的数字。在 我编写了程序的前几部分,例如:

  for row in c.execute("SELECT DIR_Dish1 FROM Tanks"):  #Main loop
    for DIR_Dish1 in row:                             #Select the column DIR_Dish1 for each entry
        if DIR_Dish1 > 0.001: 

这让我完成了任务,因为计算只涉及变量DIR_Dish1。我现在面临的问题是,我需要执行的计算,包括将DIR_Dish1乘以VAR_LevelReg, 所以我会写一些类似于:

的东西
 for row in c.execute("SELECT DIR_Radius, VAR_LevelReg, DIR_Length FROM Tanks"):
    for DIR_Radius, VAR_LevelReg, DIR_Length in row:

但是这会抛出一个int类型不是可迭代的错误。

任何帮助将不胜感激,至于我如何才能达到使用for循环输出的值30,844 ...谢谢。

1 个答案:

答案 0 :(得分:0)

for row in c.execute("SELECT DIR_Radius FROM Tanks"):
    for DIR_Radius in row:
        R2 = DIR_Radius**2
        for row in c.execute("SELECT VAR_LevelReg FROM Tanks"):
            for VAR_LevelReg in row:
                RL = DIR_Radius - VAR_LevelReg
                for row in c.execute("SELECT DIR_Length FROM Tanks"):
                    for DIR_Length in row:
                        cylinderVolume = ((( R2) * math.acos((RL ) / DIR_Radius)) - \
                        ((RL) * math.sqrt((R2) - ((RL**2))))) * (DIR_Length / 1000)
print("cyclinder Volume = ", cylinderVolume)

volume = cylinderVolume / 1000
print("Volume = ", volume)
Total = volume + dish1Volume + dish1Volume
print ("Total: ",Total)

这就是我想要的,输出:

cyclinder Volume =  30227581.910446744
Volume =  30227.581910446745
Total:  30844.130922594857

显然问题在于python中的缩进语法,由于源代码中的缩进不良,程序会多次遍历for循环。

相关问题