May-13-2020, 06:14 PM
Hi all,
I'm defining a small function as part of a larger project. This function should prompt the user for an input of the format "number number".
This input is essentially a coordinate I'll use later, but I need to make sure it fits a set of criteria. Here's what I tried to use to check the input so far, and print a few responses depending on the error in the users input:
My problem is that if the user inputs something like "x y" then I get "You should enter numbers" as expected. But even if the user inputs numbers, like "1 3", input() still keeps them as a str type. Then when I run my first check to see if they're int, "You should enter numbers!" prints as they're still held as strings.
I tried to convert the input to int once it's received, and run the test, but then if user enters x y , I get an error as the str can't be forced into an int.
Any guidance would be amazing!? This is part of an introductory course I'm taking during ye olde covid times, so I don't believe I have to do anything startlingly fancy...
Thanks,
K
I'm defining a small function as part of a larger project. This function should prompt the user for an input of the format "number number".
This input is essentially a coordinate I'll use later, but I need to make sure it fits a set of criteria. Here's what I tried to use to check the input so far, and print a few responses depending on the error in the users input:
def move_accept(z): end_ = bool() while not end_: print("Enter the coordinates: ") coords_ = input() coords_ = coords_.split() if not (type(coords_[1]) == int and type(coords_[0]) == int): print("You should enter numbers!") elif (int(coords_[0]) > 3) or (int(coords_[1]) > 3): print("Coordinates should be from 1 to 3!") elif not valid_play_finder(coords_[0], coords_[1], z): print("This cell is occupied! Choose another one!") else: end_ = True print("Move accepted") return coords_
My problem is that if the user inputs something like "x y" then I get "You should enter numbers" as expected. But even if the user inputs numbers, like "1 3", input() still keeps them as a str type. Then when I run my first check to see if they're int, "You should enter numbers!" prints as they're still held as strings.
I tried to convert the input to int once it's received, and run the test, but then if user enters x y , I get an error as the str can't be forced into an int.
Any guidance would be amazing!? This is part of an introductory course I'm taking during ye olde covid times, so I don't believe I have to do anything startlingly fancy...
Thanks,
K