School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,136 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,907 people online right now. Registration is fast and FREE... Join Now!




How do I insure that a user input is correct dollar notation?

 

How do I insure that a user input is correct dollar notation?

brianmen

7 Oct, 2009 - 01:35 PM
Post #1

New D.I.C Head
*

Joined: 14 Jun, 2009
Posts: 14



Thanked: 1 times
My Contributions
Hello, I am trying to work on a finance program, but am having trouble with some potential errors.
I want to make sure that what the user enters during a raw_input command is properly entered in US dollar notation, meaning dollars and cents. 134.11 is an example or 1523.45 also is what I'm looking to insure that my variable holding the value gets.

I'm guessing that I need some kind of while or if loop to keep asking the user to enter a proper value, but how can I check for nonsense or improper numbers? What if the user leaves the field blank or enters alphabet characters or no decimal?

I want to take the final correct value and then manipulate it using basic math as well so I know I will eventually need to use the int() command...

Please help me. Thank you!

User is offlineProfile CardPM
+Quote Post


girasquid

RE: How Do I Insure That A User Input Is Correct Dollar Notation?

7 Oct, 2009 - 01:55 PM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,648



Thanked: 58 times
Dream Kudos: 825
My Contributions
Exception handling is the easiest way to protect against this - just wrap your code in a try/except:
CODE

>>> value = raw_input('enter a dollar value: ')
enter a dollar value: no
>>> int(value)
KeyboardInterrupt
>>> try:
...  int(value)
... except:
...  print "not an integer!"
...
not an integer!

User is offlineProfile CardPM
+Quote Post

brianmen

RE: How Do I Insure That A User Input Is Correct Dollar Notation?

9 Oct, 2009 - 10:35 AM
Post #3

New D.I.C Head
*

Joined: 14 Jun, 2009
Posts: 14



Thanked: 1 times
My Contributions
QUOTE(girasquid @ 7 Oct, 2009 - 01:55 PM) *

Exception handling is the easiest way to protect against this - just wrap your code in a try/except:
CODE

>>> value = raw_input('enter a dollar value: ')
enter a dollar value: no
>>> int(value)
KeyboardInterrupt
>>> try:
...  int(value)
... except:
...  print "not an integer!"
...
not an integer!





Hmmm a bit beyond my level of Python but thanks anyway!
User is offlineProfile CardPM
+Quote Post

krzysz00

RE: How Do I Insure That A User Input Is Correct Dollar Notation?

13 Oct, 2009 - 02:15 PM
Post #4

D.I.C Head
Group Icon

Joined: 25 Feb, 2009
Posts: 78



Thanked: 2 times
Dream Kudos: 100
My Contributions
A regex along the lines of (PERL regex)
/^\$\d*\.\d*$/
If it matches it should be a correct dollar amount.


User is offlineProfile CardPM
+Quote Post

Tshiknn

RE: How Do I Insure That A User Input Is Correct Dollar Notation?

19 Oct, 2009 - 06:07 AM
Post #5

New D.I.C Head
*

Joined: 18 Oct, 2009
Posts: 18



Thanked: 2 times
My Contributions
To check if it's in dollar notation, just use the following code:

CODE
import re

valid_input = False
while valid_input == False:
    value = raw_input("What dollar value will you input? $")
    if re.match(r"[\d]+.\d\d", value):
        try:
            value = float(value)
            valid_input = True
        except TypeError:
            valid_input = False
    else:
        valid_input = False


The name 'value' now contains the dollar amount.

This post has been edited by Tshiknn: 19 Oct, 2009 - 02:58 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 02:59PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month