Python 对于用户输入,函数中是否有多个while True循环的替代方法?
我对Python比较陌生,目前正在编写第二个教程,特别是使用SQLite 在我附加的代码(特别是函数更新)中,我在这个简单函数中有3个while循环和几个break。我认为这不是一个好的做法,我正在寻求建议,以使它更容易崩溃和/或更紧凑 有太多中断语句的副作用是什么Python 对于用户输入,函数中是否有多个while True循环的替代方法?,python,sqlite,while-loop,break,Python,Sqlite,While Loop,Break,我对Python比较陌生,目前正在编写第二个教程,特别是使用SQLite 在我附加的代码(特别是函数更新)中,我在这个简单函数中有3个while循环和几个break。我认为这不是一个好的做法,我正在寻求建议,以使它更容易崩溃和/或更紧凑 有太多中断语句的副作用是什么 def updateData(): global cursor sql = 'SELECT name FROM sqlite_master WHERE type = "table" ORDER BY name'
def updateData():
global cursor
sql = 'SELECT name FROM sqlite_master WHERE type = "table" ORDER BY name'
cursor.execute(sql)
rows = cursor.fetchall()
print("These are the existing tables in the system:")
tableList = []
for row in rows:
print(row[0])
tableList.append(row[0])
while True:
table = input("Enter table to update: ")
if table in tableList:
while True:
phoneLIST = searchDB()
if len(phoneLIST) == 0:
option = tryMessage()
if not option:
break
else:
numID = int(input("Enter the ID number you want updated: "))
sql = 'PRAGMA table_info(%s)' % table
cursor.execute(sql)
rows = cursor.fetchall()
print("These are the existing columns in %s table:" % table)
colList = []
for row in rows:
print(row[1])
colList.append(row[1])
while True:
column = input("Enter the column name of ID: %d you want updated: " % numID)
column = column.upper()
if column in colList:
if column == 'BOD' or column == 'PID':
print("You can't change Birth of Date OR PID")
option = tryMessage()
if not option:
break
else:
if column == 'PHONE':
newEntry = checkPhone()
elif column == 'POSTAL':
newEntry = checkPostal()
else:
newEntry = input("Enter new information for column %s: " % column)
sql = 'UPDATE %s SET %s = "%s" WHERE PID = %d' % (table, column, newEntry, numID)
cursor.execute(sql)
displayOneEntry(numID)
commitMessage()
break
else:
print("Column not in the table")
break
break
else:
print("Table not in the database")
option = tryMessage()
if not option:
break
没有问题,而True:
循环;它们是反复做某事的自然方式,直到发生错误或用户决定退出
在我看来,这个函数有点笨拙,因为它在许多不同的细节层次上工作。最好将其重新组织为表、列ID和值的单独函数,以便每个函数只关心自己的内容,而不关心更高或更低级别的细节。我已按如下方式进行重构,消除了嵌套的while True: 再次感谢@JG
{other functions ...}
def getTable():
global cursor
sql = 'SELECT name FROM sqlite_master WHERE type = "table" ORDER BY name'
cursor.execute(sql)
rows = cursor.fetchall()
tableList = []
print("These are the available tables: ")
for row in rows:
print(row)
tableList.append(row[0])
while True:
tableName = input("Enter table to update: ")
if tableName in tableList:
return tableName
break
else:
print("Table not in the database")
# provide option to re-enter information
option = tryMessage()
if not option:
break
def getColumn(tableName, numID):
global cursor
sql = 'PRAGMA table_info(%s)' % tableName
cursor.execute(sql)
rows = cursor.fetchall()
print("These are the existing columns in %s table:" % tableName)
colList = []
for row in rows:
print(row[1])
colList.append(row[1])
while True:
colName = input("Enter the column name of ID: %d you want updated: " % numID)
colName = colName.upper()
if colName in colList:
return colName
else:
print("Column not in the table")
# provide option to re-enter information
option = tryMessage()
if not option:
break
def getID(idList):
while True:
try:
numID = int(input("Enter the ID number you want updated: "))
except ValueError:
print('Enter valid number')
continue
if numID in idList:
return numID
else:
print("Wrong ID")
# admin use only
def updateData():
global tempPassword
passWord = input("Enter password: ")
if passWord == tempPassword:
global cursor
# Displays valid tables
tableName = getTable()
idName = getIDName(tableName)
while True:
idList = searchDB()
# provide option to re-enter information
if len(idList) == 0:
option = tryMessage()
if not option:
break
else:
numID = getID(idList)
colName = getColumn(tableName, numID)
if colName == 'BOD' or colName == idName or colName == 'STATUS':
print("You can't change this field")
# provides option to re-enter information
option = tryMessage()
if not option:
break
else:
if colName == 'PHONE':
# checks format for phone input
newEntry = checkPhone()
elif colName == 'POSTAL':
# checks format for postal code input
newEntry = checkPostal()
elif colName == 'POSITION_ID':
# checks to ensure ID is valid
newEntry = checkPositionID()
else:
newEntry = input("Enter new information for column %s: " % colName)
sql = 'UPDATE %s SET %s = "%s" WHERE %s = %d' % (tableName, colName, newEntry, idName, numID)
cursor.execute(sql)
# display the updated entry for confirmation
displayOneEntry(idName, numID)
# provide option to commit changes
commitMessage(idName)
break
else:
print("Access requires correct password")
{menu ...}
谢谢你,约翰·戈登。我对代码进行了如下重构: