首页 > 解决方案 > 改进算法输入验证

问题描述

我目前使用 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

标签: pythonvalidationuser-inputcoding-efficiency

解决方案


好吧,您需要可重用性才能使这变得简单。

  1. 正如您在代码中看到的,您一直在检查一个有效的整数,直到用户输入一个。您可以创建一个简单的函数,该函数恰好执行此操作并每次都调用它

  2. 你可以对maze.is_wall(a,b)ormaze.is_wall(c,d)做同样的事情,你只是用不同的参数做同样的事情。你可以把它变成一个函数并再次调用它而不是重写代码

你的代码应该是这样的:

1. getValidValue(a)
2. getValidValue(b)
3. isWallValid(a,b)
......

推荐阅读