我目前已经使用Maze类中的(.iswall(((函数为用户定义的起始坐标编码了一个输入验证,以确保输入是一个整数,并且它不在墙内。对于另外两组坐标,这将再次重复,这两组坐标将成为迷宫的终点。我觉得这是一种混乱的意大利面条式编程方式,我想知道是否有更好的方法?
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)
......