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

2024-06-25 23:54:47 发布

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

我使用的是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。我现在面临的问题是,我需要执行的计算,包括将say 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类型是不可编辑的错误

任何帮助都将不胜感激,关于我如何才能达到30844的价值。。。正在使用for循环输出。多谢各位


Tags: infromforexecutevardirselectlength
1条回答
网友
1楼 · 发布于 2024-06-25 23:54:47
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循环中多次迭代

相关问题 更多 >