python - 在用户输入的函数中是否有多个 while True 循环的替代方法?
问题描述
我对 Python 比较陌生,目前正在编写第二个教程,特别是使用 SQLite。
在我附加的代码(特别是函数更新)中,我在这个简单的函数中有 3 个 while 循环和几个中断。我认为这不是一个好的做法,我正在寻找建议以使其不易崩溃和/或更紧凑。
有太多的 break 语句有什么副作用?
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
解决方案
我进行了如下重构,消除了嵌套的 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 ...}
推荐阅读
- javascript - 当我使用 expo 在手机中运行代码时,React Native 出现此错误
- python - Python,输入未设置变量
- javascript - 如何使用 CSS Selector 在特定元素之后选择元素?
- excel - Excel 和 VBA 给出不同的答案
- javascript - jQuery没有将类添加到选定的div id
- nat - Nat / 重定向 / 转发端口 Mikrotik
- kubernetes - Kubernetes 卷未安装
- audio - 渲染动画(视频)时,Blender VSE 音频不同步
- database - 在 AWS 数据库迁移服务中出现外键破坏错误
- java - 如何在 Selenium 中检查表单元素的有效性状态