7 Replies - 12716 Views - Last Post: 03 May 2011 - 10:54 AM Rate Topic: -----

#1 successfulangel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-May 11

how can i use isdigit()?

Posted 03 May 2011 - 01:16 AM

I am a beginner of Python language.I use input function to ask number from user and i want to show error message when user make space(by pressing enter key )or putting string.But i can't check my error.Here is my coding.
ask=input("Enter your number")
if ask=="":
print"invalid guess"
if ask==str:
print"invalid guess"
What wrong my coding and i want to know my fault.But some say we can use isdigit() to catch this error.But i am not familiar with isdigit() function.So please guide me the way that i can check my error.Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: how can i use isdigit()?

#2 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: how can i use isdigit()?

Posted 03 May 2011 - 02:42 AM

Hi,
Maybe it would be easier if you tried to make the number an integer using the int() (or a float()) and then catch the ValueError if it is not a proper number. If you really want the type, you can use type() and it will reply the type, but something like this might be easier:
ask=input("Enter your number")
if ask=="":
    print"invalid guess"
try:
    new_ask = int(ask) #if you are expecting floats, use the line below
    #new_ask = float(ask)
except ValueError:
    print"invalid guess"


Hope I could help!
Was This Post Helpful? 0
  • +
  • -

#3 successfulangel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-May 11

Re: how can i use isdigit()?

Posted 03 May 2011 - 02:54 AM

Hi,
Thanks your reply.But i can't compile the program although i use your code.If it is possible i want to use isdigit().
i think if we use input function,we can't ask ""(space)or string from the user and if user make this error,our program show error,so i want to know other way if it is possible.Thanks a lot.
Was This Post Helpful? 0
  • +
  • -

#4 CryptochildXR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-January 10

Re: how can i use isdigit()?

Posted 03 May 2011 - 03:34 AM

well clearly you want to check whether the input is an empty space or a string... i was doing this in Python 3.1 and it goes like this

def ask_code():
	ask = input(" enter your number ") #declare your pin variable
	if ask == " ":
		print('Invalid') #the condition  for space to give invalid
	try:  # simillar to the if-else statements but its try-except
		int(ask) # if its not convertible to int then its a string
	except:  # simillar to the if-else statements but its try-except
		print('invalid') # if not converted then print "invalid"
	else:
		print('granted')



the
try
and
except
are like if and else mostly used for when the we want to overide an error with a message of our own...

like in this code we try to convert to int except if not possible we print invalid
Was This Post Helpful? 0
  • +
  • -

#5 CryptochildXR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-January 10

Re: how can i use isdigit()?

Posted 03 May 2011 - 03:45 AM

or you can just say
if ask.isdigit():
	print "access granted"
else:
	print "invalid"

Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: how can i use isdigit()?

Posted 03 May 2011 - 06:44 AM

I'm going to have to disagree with you Eric115. Using a try/except block takes much more overhead than simply using the isdigit method.
while True:
    ask=input("Enter your number")
    if ask.isdigit() == False:
        print("invalid guess")
    else:
        break




EDIT:
Yep, I merged 2.x and 3.x for a second there.

This post has been edited by atraub: 03 May 2011 - 10:54 AM

Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,628
  • Joined: 16-October 07

Re: how can i use isdigit()?

Posted 03 May 2011 - 07:42 AM

Alas, that code won't work in any python. The input will fail in python2, the print in python3. Let's test:

Tried in python2:
>>> while True:
...     ask=input("Enter your number")
...     if ask.isdigit() == False:
...         print"invalid guess"
...     else:
...         break
... 
Enter your number123
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
AttributeError: 'int' object has no attribute 'isdigit'
>>> 



Python 3 (I fixed the print):
>>> while True:
...     ask=input("Enter your number")
...     if ask.isdigit() == False:
...             print("invalid guess")
...     else:
...             break
... 
Enter your number123
>>> ask
'123'
>>> 



While this doesn't choke, it also gives me a string...

For something portable across versions, you probably need to do a catch, if not for a value error, then for a NameError.
Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: how can i use isdigit()?

Posted 03 May 2011 - 10:54 AM

aww you got me, I meant to say Python 3 (I use 2.x at work so sometimes that trickles through).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1