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


Messages In This Thread
Accepting inputs str vs int - by lbtdne - May-13-2020, 06:14 PM
RE: Accepting inputs str vs int - by deanhystad - May-13-2020, 07:41 PM
RE: Accepting inputs str vs int - by lbtdne - May-14-2020, 01:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with accepting multiple string inputs Ryan_Todd 5 2,964 Jan-22-2020, 06:12 PM
Last Post: buran
  signal.itimer() not accepting keyword arguments Skaperen 2 2,253 Dec-19-2019, 12:14 AM
Last Post: Skaperen
  Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy) Drone4four 4 3,804 Aug-23-2019, 07:59 PM
Last Post: Drone4four
  Sqlalchemy accepting ISO 8601 KirkmanJ 0 3,590 Jul-27-2018, 01:52 PM
Last Post: KirkmanJ
  MySQL not accepting utf8mb4. Warning 3719 jonesin1974 0 4,779 Jun-21-2018, 03:09 AM
Last Post: jonesin1974
  if loop is not accepting && A_Embedded 2 2,761 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