防止WHILE循环在python3

时间:2016-11-24 20:41:51

标签: python python-3.x while-loop

第一次问问,长时间潜伏。我是Python的新爱好者,所以请原谅我的无知。我正在尝试使用"而#34;重新启动我的脚本。环。我重新启动就好了,但我不希望以前运行的代码保留以前的任何值。

这里发生的是我进入两个位置以获得距离(如笛卡尔平面方程式),它引用数据库来确定一些数据。当代码通过while循环时,它不会要求新的位置。我有几个不同的py文件(模块可能是正确的单词)被引用。代码如下。请原谅一塌糊涂:

#Mission Processor
from gridgrab import gridgrab
from Range import range
from table_f_reader import interpolate_elev, read_elev, bracketer
import sqlite3
import math

conn = sqlite3.connect ('chg_1L.db')
c = conn.cursor()
loop_back = 'y'
while loop_back == 'y':
    cht_range = int(round(range(), 0))
    print(cht_range, "meters")
    if cht_range % 100 == 0:
        elev = read_elev(cht_range)
    else:
        elev = interpolate_elev(cht_range)
    print('The range', cht_range, 'gives elevation', elev)
    loop_back = input('Do you want to run another mission? Enter Y or N:')

这是不受欢迎的输出:

Battery Location: 1230
Target Location: 1233
Battery Grid: 12000 30000
Target Grid: 12000 33000
3000 meters
The range 3000 gives elevation 172.0
Do you want to run another mission? Enter Y or N:y
3000 meters
The range 3000 gives elevation 172.0
Do you want to run another mission? Enter Y or N:y
3000 meters
The range 3000 gives elevation 172.0
Do you want to run another mission? Enter Y or N:n

感谢您阅读所有这些以及您的耐心。

修改

这里是上面代码导入的其他.py文件。第一个是距离计算器:

#MGRS Distance Calculator
import math
from gridgrab import gridgrab
from gridfix import gridfix
grids=gridgrab()
x1 = gridfix(int(grids[0]))
y1 = gridfix(int(grids[1]))
x2 = gridfix(int(grids[2]))
y2 = gridfix(int(grids[3]))
print('Battery Grid:', x1, y1)
print('Target Grid:', x2, y2)

def range():
    global x1, x2, y1, y2
    range = math.sqrt(((x2 - x1)**2) + ((y2 - y1)**2))
    return range

这是一个单独的py文件。它本质上是一个数据库阅读器。     import sqlite3     导入数学     来自范围导入范围

def bracketer(x):     #function to bracket values
    global a, b
    a = math.floor(x/100)*100
    b = math.ceil(x/100)*100
    return a, b

def read_elev(range): #no interpolation required
    conn = sqlite3.connect ('chg_1L.db')
    c = conn.cursor()
    entry_range = (range, )
    c.execute('SELECT range, elev FROM tableF WHERE range = ?', entry_range)
    row = c.fetchone()
    z, elev = row #z is throw away
    c.close()
    conn.close()
    return elev

def interpolate_elev(range):
    conn = sqlite3.connect ('chg_1L.db')
    c = conn.cursor()
    bracketer(range) #bracket vaules and create tuple for database entry
    bracket = (a, b)
    c.execute('SELECT range, elev FROM tableF WHERE range = ? OR range = ?', bracket) #pull ranges and elevations from database.
    row = c.fetchall()
    lower = row[0] #pick out lower and upper data
    upper = row[1]
    lower_rg, lower_elv  = lower #unpack tuple
    upper_rg, upper_elv = upper
    inner_rg = range - lower_rg #interpolate
    outer_rg = upper_rg - lower_rg
    outer_elv = upper_elv - lower_elv
    x = inner_rg * outer_elv / outer_rg
    inner_elv = round(x, 1)
    elev = lower_elv + inner_elv
    c.close()
    conn.close()
    return elev

0 个答案:

没有答案