Parsing Data Types

This is the place for queries that don't fit in any of the other categories.

Parsing Data Types

Postby dday9 » Thu Oct 17, 2013 7:39 pm

Currently right now what I'm doing to convert a string to an integer, I'm using this:
Code: Select all
i = int(input("Please enter in a number: "))


However, if I enter in a character that's non-numeric the program crashes because int() doesn't actually parse the string that's inputted, it just trys to convert the string to an integer. What I'm looking for is something like this:

Code: Select all
#pseudo code:
response = input("Please enter in a number: ")
if response not numeric:
   print ("Invalid Input.")
else:
   i = int(response)


Or if I were to go back to my vb.net code, it would be TryParse:
Code: Select all
Console.WriteLine("Please enter in a number: ")
Dim i As Integer
Dim response As String = Console.ReadLine()
If Integer.TryParse(response, i) Then
   'Preform rest of code
Else
   Console.WriteLine("Invalid Input.")
End If
dday9
 
Posts: 17
Joined: Fri Oct 11, 2013 8:55 pm

Re: Parsing Data Types

Postby stranac » Thu Oct 17, 2013 7:49 pm

You should use a try/except block for this:
Code: Select all
response = input("Please enter in a number: ")
try:
    i = int(response)
except ValueError:
    print ("Invalid Input.")
else:
    # continue doing your stuff


You can get more information about exception handling here: http://docs.python.org/2/tutorial/errors.html
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Parsing Data Types

Postby dday9 » Thu Oct 17, 2013 8:17 pm

Ah ok. I know, at least in vb.net, using exception handling like that it tends to slow down the program(sometimes dramatically) is this the same in Python or not so much?
dday9
 
Posts: 17
Joined: Fri Oct 11, 2013 8:55 pm

Re: Parsing Data Types

Postby stranac » Thu Oct 17, 2013 8:23 pm

No, exception handling performs pretty well.
It is in most cases slower to explicitly check for error conditions.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Parsing Data Types

Postby dday9 » Thu Oct 17, 2013 8:29 pm

Alrighty, thanks a bunch!
dday9
 
Posts: 17
Joined: Fri Oct 11, 2013 8:55 pm

Re: Parsing Data Types

Postby micseydel » Thu Oct 17, 2013 10:43 pm

If you're validating user input, and your program is slower than the user, then it's definitely not Python's exceptions' fault. Also, for the most part we're more concerned with nice code than a "fast" program, and exceptions often use nicer code and are more robust. (That said, if you expect your exception to happen rarely, your program will be much faster with exception handling.)
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1507
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 6 guests