Reputation: 9
I've currently coded an input validation for a user defined start co ordinate to ensure that the input is an integer and that it is not inside a wall using an (.iswall()) function from my Maze class. This repeats again for another two sets of co ordinates , which is intended to be the end point of the maze. I feel like this is a messy spaghetti way of programming and was wondering if there was a much better way of doing this?
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
Upvotes: 0
Views: 59
Reputation: 562
Well, you need reusability to make this simple.
As you can see in your code, you are constantly checking for a valid integer until the user enters one. You can just create a simple function that just exactly does that and calls it every time
You can do the same for maze.is_wall(a,b)
or maze.is_wall(c,d)
, you are exactly doing the same thing with just different parameters. You can turn this into a function and call it again instead of rewriting the code
Your code should become something like this:
1. getValidValue(a)
2. getValidValue(b)
3. isWallValid(a,b)
......
Upvotes: 1