简体   繁体   English

如何正确退出while循环?

[英]How to exit while loop properly?

I have a program that runs through a list of names in 'serverlist.txt'. 我有一个程序贯穿“ serverlist.txt”中的名称列表。 The user selects the database they want to search in by choosing option 1 or option 2. The program will run through all names in the list and provide the id tied to each name. 用户通过选择选项1或选项2来选择要搜索的数据库。该程序将遍历列表中的所有名称,并提供与每个名称相关联的ID。

Name: Jupiter ID: 23 Name: Mars ID: 26 Name: Mercury ID: 27 名称:木星ID:23名称:火星ID:26名称:水星ID:27

This works fine but it doesn't stop. 这工作正常,但不会停止。 When the list is complete, it loops through everything all over again. 列表完成后,它将再次遍历所有内容。

How do I stop it from going through the list more than once? 如何阻止它多次浏览列表?

import pypyodbc
import os

def replaceid(connection, servername):
    try:
        cursor = connection.cursor()

        SQLCommand = ("SELECT Name, Location_ID "
            "FROM dbo.Server_ID "   # table name
            "with (nolock)"
            "WHERE Name = ?")
        Values = [servername]
        cursor.execute(SQLCommand,Values)
        results = cursor.fetchone()
        if results:

            print (" Name: " + results[0] + " ID: " + str(results[1]))
            print (" ")
            locationid(results, connection, servername)
        else:
            print (" ID for " + servername + " does not exist.")
            print (" ")
            connection.close()
    except:
        print("Database is down or you are not connected to network.")
        exit()

def start1():

    os.system('cls' if os.name == 'nt' else 'clear')
    array = []
    local = input('\n\n Type option 1 or 2: ')
    while True:
        with open("serverlist.txt", "r") as f:
            for servername in f:
                try:                

                    if local in ['1']:
                        connection = pypyodbc.connect('Driver={SQL Server};Server=db1;Database=WinOasis;Trusted_Connection=yes;')
                    elif local in ['2']:
                        connection = pypyodbc.connect('Driver={SQL Server};Server=db2;Database=WinOasis;Trusted_Connection=yes;')
                    else:
                        return
                except pypyodbc.Error as ex:
                    sqlstate = ex.args[0]
                    if sqlstate == '28000':
                        print ("You do not have access.")

                replaceid(connection, servername.strip())
    return

start1()

I think your return statement on the third to last line needs to be indented one level. 我认为您在第三行到最后一行的return语句需要缩进一个级别。 Otherwise your while loop will run forever, because True will always be true! 否则,您的while循环将永远运行,因为True永远都是true!

You might want to add a break statement after you call replaceid(connection, servername.strip()) in start1() 您可能要在start1()调用replaceid(connection, servername.strip())后添加一个break语句

You might also want a break statement after the exception clause ends. 您可能还需要在exception子句结束后使用break语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM