Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accepting inputs str vs int
#1
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:


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
Reply
#2
input returns a str, so no matter what characters you type, the type will never be an integer.

The normal way to solve this problem is assume the user enters integers and capture the exception that gets thrown if they type something else.
def move_accept(z):
    while True:
        try:
            coords = list(map(int, input('Enter the coordiantes: ').split()))
            if len(coords) != 2:
                print('Please enter two numbers')
            elif not ((0 < coords[0] <= 3) and (0 < coords[1] <= 3)):
                print('Coordinates must be in rage 1..3')
            #elif playfinder test fails
                #print playfinder fail message
            else:
                return coords
        except ValueError:
            print('Please enter two numbers')

print(move_accept(10))
Reply
#3
Thanks for your help @deanhystad . I had come across the try/except set but I'd assumed as it hadn't been covered in the course I'm working through that I wouldn't need it. I've posted my final, working function below and marked solved.
def move_accept(z):
    while True:
        try:
            print("Enter the coordinates: ")
            coords_ = input()
            coords_ = coords_.split()
            if not ((0 < int(coords_[0]) <= 3) and (0 < int(coords_[1]) <= 3)):
                print("Coordinates should be from 1 to 3!")
            elif not valid_play_finder(int(coords_[0]), int(coords_[1]), z):
                print("This cell is occupied! Choose another one!")
            else:
                return coords_
        except ValueError:
            print("You should enter numbers!")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with accepting multiple string inputs Ryan_Todd 5 2,875 Jan-22-2020, 06:12 PM
Last Post: buran
  signal.itimer() not accepting keyword arguments Skaperen 2 2,195 Dec-19-2019, 12:14 AM
Last Post: Skaperen
  Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy) Drone4four 4 3,716 Aug-23-2019, 07:59 PM
Last Post: Drone4four
  Sqlalchemy accepting ISO 8601 KirkmanJ 0 3,542 Jul-27-2018, 01:52 PM
Last Post: KirkmanJ
  MySQL not accepting utf8mb4. Warning 3719 jonesin1974 0 4,724 Jun-21-2018, 03:09 AM
Last Post: jonesin1974
  if loop is not accepting && A_Embedded 2 2,705 Apr-22-2018, 02:24 PM
Last Post: A_Embedded

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020