改进算法输入验证
Improving algorithm input validation
我目前已经为用户定义的起始坐标编写了输入验证代码,以确保输入是一个整数并且它不在墙内,使用我的迷宫中的 (.iswall()) 函数 class。对于另外两组坐标再次重复此操作,其旨在作为迷宫的终点。我觉得这是一种混乱的意大利面条式编程方式,想知道是否有更好的方法来做到这一点?
while True: #While loop that acts as validation to ensure the user enters correct data
a = input("Enter x coordinate of the place you'd like to start: ") #Allow the user to enter a co ordinate for the starting x value
while a.isdigit() is False: #If the input they enter is not an integer, keep them in a while loop until it is
a = input("Enter x coordinate of the place you'd like to start(must be an integer): ") #Prompt the user again to enter a valid x co ordinate
a = int(a) #Finally make the value an integer in memory
b = input("Enter y coordinate of the place you'd like to start: ") #Allow the user to enter a co ordinate for the starting y value
while b.isdigit() is False: #Ensure that the value they enter is a digit and nothing else
b = input("Enter y coordinate of the place you'd like to start(must be an integer): ") #Keep prompting the user to enter a co ordinate until it is an integer
b = int(b) #Make the y co ordinate an integer value in memory
while maze.is_wall(a, b): #This is another validation to ensure that the starting co ordinates are not in a wall.
print("Sorry, those starting co ordinates are inside a wall, please select a new one") #Tell the user that this is not a valid starting point
a = input("Enter x coordinate of the place you'd like to start: ") #Prompt the user again to enter an x co ordinate
while a.isdigit() is False: #Again check the valilidity of the data inputted and ensure it is an integer
a = input("Enter x coordinate of the place you'd like to start(must be an integer): ") #If it is not an integer, prompt them to enter a valid integer this time with extra validation to ensure it is not inside a wall
a = int(a) #After meeting the conditions, store the integer value of it into memory
b = input("Enter y coordinate of the place you'd like to start: ") # Allow the user to enter a y co ordinate
while b.isdigit() is False: #If the value inputed is not an integer
b = input("Enter y coordinate of the place you'd like to start(must be an integer): ") #Keep prompting the user to enter a valid co ordinate, this time with extra validation to ensure it is not inside a wall
b = int(b) #After meeting the conditions, store the integer value of the y co ordinate in memory
c = input("Enter x coordinate of the place you'd like to end: ") #Allow the user to enter a co ordinate for the starting x value
while c.isdigit() is False: #Validation toe nsure the input is an integer
c = input("Enter x coordinate of the place you'd like to end(must be an integer): ") #If it is isn't an integer then prompt the user to enter a valid integer again
c = int(c) #After meeting the conditions, store the integer value of it into memory
d = input("Enter y coordinate of the place you'd like to end: ") #Allow the user to enter a co ordinate for the ending y value
while d.isdigit() is False: #Check if the inputted value is an integer
d = input("Enter y coordinate of the place you'd like to end(must be an integer): ") #If it isn't an integer, tell the user that it must be an integer and prompt them to input a valid coordinate
d = int(d) #After meeting the conditions, store the integer value of it into memory
while maze.is_wall(c, d): #Validation to ensure that the end point is not inside a wall
print("Sorry, those end co ordinates are inside a wall, please select a new one") #Tell the user that this is not a valid end point
c = input("Enter x coordinate of the place you'd like to end: ") #Prompt the user again to enter an x co ordinate
while c.isdigit() is False: #Check that the valid inputted is an integer
c = input("Enter x coordinate of the place you'd like to end(must be an integer): ") #If it isn't an integer, tell the user that it must be an integer and prompt them to input a valid coordinate
c = int(c) #After meeting the conditions, store the integer value of it into memory
d = input("Enter y coordinate of the place you'd like to end: ") #Allow the user to enter a co ordinate for the ending y value
while d.isdigit() is False: #Check if the inputted value is an integer
d = input("Enter y coordinate of the place you'd like to end(must be an integer): ") #If it isn't an integer, tell the user that it must be an integer and prompt them to input a valid coordinate
d = int(d) #After meeting the conditions, store the integer value of it into memory
好吧,您需要可重用性才能使这变得简单。
如您在代码中所见,您不断检查有效整数,直到用户输入一个整数。您可以只创建一个简单的函数,它正好可以做到这一点,并且每次都调用它
您可以对 maze.is_wall(a,b)
或 maze.is_wall(c,d)
做同样的事情,只是参数不同而已。你可以把它变成一个函数再调用它而不是重写代码
你的代码应该变成这样:
1. getValidValue(a)
2. getValidValue(b)
3. isWallValid(a,b)
......
我目前已经为用户定义的起始坐标编写了输入验证代码,以确保输入是一个整数并且它不在墙内,使用我的迷宫中的 (.iswall()) 函数 class。对于另外两组坐标再次重复此操作,其旨在作为迷宫的终点。我觉得这是一种混乱的意大利面条式编程方式,想知道是否有更好的方法来做到这一点?
while True: #While loop that acts as validation to ensure the user enters correct data
a = input("Enter x coordinate of the place you'd like to start: ") #Allow the user to enter a co ordinate for the starting x value
while a.isdigit() is False: #If the input they enter is not an integer, keep them in a while loop until it is
a = input("Enter x coordinate of the place you'd like to start(must be an integer): ") #Prompt the user again to enter a valid x co ordinate
a = int(a) #Finally make the value an integer in memory
b = input("Enter y coordinate of the place you'd like to start: ") #Allow the user to enter a co ordinate for the starting y value
while b.isdigit() is False: #Ensure that the value they enter is a digit and nothing else
b = input("Enter y coordinate of the place you'd like to start(must be an integer): ") #Keep prompting the user to enter a co ordinate until it is an integer
b = int(b) #Make the y co ordinate an integer value in memory
while maze.is_wall(a, b): #This is another validation to ensure that the starting co ordinates are not in a wall.
print("Sorry, those starting co ordinates are inside a wall, please select a new one") #Tell the user that this is not a valid starting point
a = input("Enter x coordinate of the place you'd like to start: ") #Prompt the user again to enter an x co ordinate
while a.isdigit() is False: #Again check the valilidity of the data inputted and ensure it is an integer
a = input("Enter x coordinate of the place you'd like to start(must be an integer): ") #If it is not an integer, prompt them to enter a valid integer this time with extra validation to ensure it is not inside a wall
a = int(a) #After meeting the conditions, store the integer value of it into memory
b = input("Enter y coordinate of the place you'd like to start: ") # Allow the user to enter a y co ordinate
while b.isdigit() is False: #If the value inputed is not an integer
b = input("Enter y coordinate of the place you'd like to start(must be an integer): ") #Keep prompting the user to enter a valid co ordinate, this time with extra validation to ensure it is not inside a wall
b = int(b) #After meeting the conditions, store the integer value of the y co ordinate in memory
c = input("Enter x coordinate of the place you'd like to end: ") #Allow the user to enter a co ordinate for the starting x value
while c.isdigit() is False: #Validation toe nsure the input is an integer
c = input("Enter x coordinate of the place you'd like to end(must be an integer): ") #If it is isn't an integer then prompt the user to enter a valid integer again
c = int(c) #After meeting the conditions, store the integer value of it into memory
d = input("Enter y coordinate of the place you'd like to end: ") #Allow the user to enter a co ordinate for the ending y value
while d.isdigit() is False: #Check if the inputted value is an integer
d = input("Enter y coordinate of the place you'd like to end(must be an integer): ") #If it isn't an integer, tell the user that it must be an integer and prompt them to input a valid coordinate
d = int(d) #After meeting the conditions, store the integer value of it into memory
while maze.is_wall(c, d): #Validation to ensure that the end point is not inside a wall
print("Sorry, those end co ordinates are inside a wall, please select a new one") #Tell the user that this is not a valid end point
c = input("Enter x coordinate of the place you'd like to end: ") #Prompt the user again to enter an x co ordinate
while c.isdigit() is False: #Check that the valid inputted is an integer
c = input("Enter x coordinate of the place you'd like to end(must be an integer): ") #If it isn't an integer, tell the user that it must be an integer and prompt them to input a valid coordinate
c = int(c) #After meeting the conditions, store the integer value of it into memory
d = input("Enter y coordinate of the place you'd like to end: ") #Allow the user to enter a co ordinate for the ending y value
while d.isdigit() is False: #Check if the inputted value is an integer
d = input("Enter y coordinate of the place you'd like to end(must be an integer): ") #If it isn't an integer, tell the user that it must be an integer and prompt them to input a valid coordinate
d = int(d) #After meeting the conditions, store the integer value of it into memory
好吧,您需要可重用性才能使这变得简单。
如您在代码中所见,您不断检查有效整数,直到用户输入一个整数。您可以只创建一个简单的函数,它正好可以做到这一点,并且每次都调用它
您可以对
maze.is_wall(a,b)
或maze.is_wall(c,d)
做同样的事情,只是参数不同而已。你可以把它变成一个函数再调用它而不是重写代码
你的代码应该变成这样:
1. getValidValue(a)
2. getValidValue(b)
3. isWallValid(a,b)
......