Input validaion

A forum for general discussion of the Python programming language.

Input validaion

Postby Deaf Smith » Mon Feb 03, 2014 5:34 am

Hello,

This is my first post.

Now I'm trying to find a way to validate decimal input (that is stop non-numeric input, but allow decimal points or integer numbers.)

I found away to get integer validation but not floating point decimal.

any guidance would be appreciated.

Thanks,

Deaf
Last edited by micseydel on Mon Feb 03, 2014 5:43 am, edited 1 time in total.
Reason: Locked OP.
Deaf Smith
 
Posts: 3
Joined: Mon Feb 03, 2014 4:00 am

Re: Input validaion

Postby micseydel » Mon Feb 03, 2014 5:45 am

Hello, and welcome to the forum! Make sure to read this if you haven't already, and if you have, do it another pass over.

If you're doing your integer validation in the typical way, then replacing the int call with a float call will be the quickest way to the solution. Otherwise, we'd like to see what you already have so that we can give you the best advice.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1115
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Input validaion

Postby Deaf Smith » Mon Feb 03, 2014 9:04 pm

Ok, I am trying to input variables.

All will end up being floats but..

if the user input is anything else, character or garbage, I need to reject and force them to re-do their input.

It then multiples by .001 to shift the decimal point across (for calculating the area and radius of a circle.)




Code: Select all
def main():
# get the diameter (as interger then covert)
  while True:
    x = raw_input("Enter the diameter in inches: ")
    if x.isdigit():
         break
    print "Try again, value you entered was not an integer."
  BulDia = 0.0
  float(BulDia)
  BulDia = x
  BulDia = BulDia * .001

****

This code above does not work. Gives me a

BulDia = BulDia * .001
TypeError: can't multiply sequence by non-int of type 'float'

Thanks.
Last edited by metulburr on Mon Feb 03, 2014 9:13 pm, edited 1 time in total.
Reason: added code tags
Deaf Smith
 
Posts: 3
Joined: Mon Feb 03, 2014 4:00 am

Re: Input validaion

Postby metulburr » Mon Feb 03, 2014 9:13 pm

Please reread mics link he gave to learn how to post code in code tags.

Code: Select all
float(BulDia)

the built in float returns the float value, thus you need to save it somewhere.

Code: Select all
x = raw_input("Enter the diameter in inches: ")
...
BulDia = x
BulDia = BulDia * .001

x is of type string
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1300
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Input validaion

Postby stranac » Mon Feb 03, 2014 9:21 pm

Also, x.isdigit() doesn't check if the input is a float...it doesn't even check if it's an int.
It would miss numbers which should be perfectly fine, e.g. +3

The way to make sure the user entered a number is to try converting the input into one.
If it fails, the input wasn't a number.

Code: Select all
>>> while True:
...     x = raw_input('Enter a number: ')
...     try:
...         x = float(x)
...     except ValueError:
...         print 'That is not a number!'
...     else:
...         break
...
Enter a number: a number
That is not a number!
Enter a number: that was a number
That is not a number!
Enter a number: Dude, you said enter "a number"
That is not a number!
Enter a number: a number
That is not a number!
Enter a number: 63.99e6
>>> x
63990000.0
Friendship is magic!

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

Re: Input validaion

Postby Deaf Smith » Wed Feb 05, 2014 3:49 am

Thanks.

Will this work with 2.7 Python?

That is what I use with IDLE on Ubuntu Linux (on my other machine that is occupied right now.)

Deaf
Deaf Smith
 
Posts: 3
Joined: Mon Feb 03, 2014 4:00 am

Re: Input validaion

Postby Mekire » Wed Feb 05, 2014 6:00 am

The code you have been shown is for python 2.x. The function raw_input does not exist in python 3 (or rather it was renamed to input and input was removed).

-Mek
User avatar
Mekire
 
Posts: 978
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests