4 Replies - 344 Views - Last Post: 28 November 2012 - 12:47 PM Rate Topic: -----

#1 littlePilot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 07-March 12

how to get this error to work

Posted 26 November 2012 - 03:47 PM

d_or_p = 3


if d_or_p in (range (3,4)):
    PH = -1
    while PH == -1:
        blaa = int(input ("what is your phone number?"))
        if len(str(blaa)) in range(12) and blaa >= 0:
            PH = 1
        else:
            print ("Please enter number under 5")      
        

print ("woot")


how do i make this handle the error?
Is This A Good Question/Topic? 0
  • +

Replies To: how to get this error to work

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,105
  • Joined: 19-March 11

Re: how to get this error to work

Posted 26 November 2012 - 03:54 PM

It would be nice if you'd tell us what you're expecting to see and what you're seeing that you don't expect to see, so we know what error you might be talking about.

What exactly are you trying to do here?
Was This Post Helpful? 0
  • +
  • -

#3 littlePilot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 07-March 12

Re: how to get this error to work

Posted 26 November 2012 - 03:59 PM

sorry about that.... i want it that if a question asked before = 3 then do this statement if not it will just leave it... but if its 3. then entire a phone number and i want to to just except a int not anything else and until a number is entered it keeps looping.

i get a value error coz "sggfdgf" is not an int

does that help or? O.o
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,105
  • Joined: 19-March 11

Re: how to get this error to work

Posted 26 November 2012 - 04:14 PM

Phone numbers aren't ints. US format is ddd-dddd, optionally preceded by an area code which may optionally be enclosed in parentheses.

So ideally you'd like to have a regular expression capturing all of that - that's more than I want to put together right not, but you might be able to find it out there on the world wide whatever. But you could probably get away with one of a few hacks.

For example, you could simply say "enter a phone number, with no punctuation"

In that case you're expecting a string consisting of decimal digits only, so you can use the str.isdigit() function, which returns true for a string if each character in that string is a decimal digit.

Or you could specify a format like "ddd-ddd-dddd or "ddd-dddd", and split on the hyphen. Now you have a list of strings, each of which must be composed of digits, so use the same test as before.

Or if you really want to recognize the parens form, ie (505)252-2323, you could do something horrible like

firstsplit = phonenumber.split(")")
if firstsplit[0][1:].isdigit() == false:
   return false
for part in firstdigit[1].split("-"):
   if part.isdigit() == false:
   return false
return true



But that sort of code is always a sign that things have gone terribly wrong, and you should back up and learn about regular expressions or something.
Was This Post Helpful? 0
  • +
  • -

#5 Hqtitan  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 51
  • Joined: 28-August 10

Re: how to get this error to work

Posted 28 November 2012 - 12:47 PM

The value error is because you're trying to convert the input straight to an int without checking whether the input consists of only digits. If you try to convert something containing any characters other than digits or a decimal, it will give you a value error. You should check that the input contains only digits, using the isdigit() string method, before you try to convert it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1