![](/img/trans.png)
[英]Running try and except in a function and re-running results in a NoneType return. How do I fix this?
[英]I an sending 3 values to a function but after the function I just want to test agains the 3th function return. How can I do this?
rows = 22
rowsMax = 21
columns = 117
columnMax = 117
status = "True"
def DrawBoard (rows,columns,status):
if rows > rowsMax or columns > columnMax:
status = "False"
rows = 0
columns = 0
return rows,columns,status
DrawBoard(rows,columns,status)
# I want to test only the status returns from the function but get error
# DrawBoard() missing 2 required positional arguments: 'columns' and 'status'.
if DrawBoard(status) == "False":
print("Either the row and columns maximum were excepted")
如果您編寫DrawBoard(.., .., ..)
那是一個函數調用,因此您正在調用該函數並返回其返回值。 要保存這些值,您可以這樣做
ret1, ret2, ret3 = DrawBoard(rows, columns, status)
現在檢查第三個值
if ret3 == False:
您還可以直接訪問第三個返回值,而無需使用DrawBoard(rows, columns, status)[2]
將它們存儲在變量中。
推送到對象版本:
class Board:
def __init__(self, rows, rowsMax, columns, columnMax):
self.rows = rows
self.rowsMax = rowsMax
self.columns = columns
self.columnMax = columnMax
self.status = True
def drawBoard(self):
msgError = 'Either the row and columns maximum were excepted'
if self.rows > self.rowsMax or self.columns > self.columnMax:
self.status = False
self.rows = 0
self.columns = 0
else:
msgError = 'All fine!'
return msgError
if __name__ == '__main__':
newBoard = Board(22, 21, 117, 117)
print(newBoard.drawBoard()) # Either the row and columns maximum were excepted
print(newBoard.status) # False
newBoard = Board(20, 21, 117, 117)
print(newBoard.drawBoard()) # All fine!
print(newBoard.status) # True
訪問狀態就像self.status
在類中的 True 或 False 一樣簡單,或者使用帶有newBoard.status
的對象......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.