7 Replies - 663 Views - Last Post: 12 April 2011 - 01:56 AM Rate Topic: -----

#1 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Input check in Python

Posted 11 April 2011 - 01:44 AM

Hi guys,

I want to do an input check to the characters that the user enters .
The only permitted inputs are Integers , i.e. ,if the user enters a series
that has at least one Space (" ") or at least one character other than the digits 0-9
then the Input above is incorrect.

So why not do it with
choice=input()
?
In that way I get a string (str) , right ? but how can i check each and every character
in the string ? because the string is not a list ,and isn't an array .

ideas ?
Thanks,Ron

Is This A Good Question/Topic? 0
  • +

Replies To: Input check in Python

#2 Nallo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 161
  • View blog
  • Posts: 247
  • Joined: 19-July 09

Re: Input check in Python

Posted 11 April 2011 - 03:06 AM

View Postpasser_by, on 11 April 2011 - 02:44 AM, said:

but how can i check each and every character in the string ?

You can loop through a string just like you loop through a list:
>>> for c in "kli":
	print(c)

k
l
i


and ckeck those characters to see if they are in "0123456789"
>>> "a" in "01234567890"
False
>>> "4" in "0123456789"
True


Was This Post Helpful? 1
  • +
  • -

#3 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Input check in Python

Posted 11 April 2011 - 04:52 AM

Another option would be to cast to an integer and catch a ValueError exception.

choice = input()
try:
  choice = int(choice)
except ValueError as ex:
  print("Bad input")


Was This Post Helpful? 1
  • +
  • -

#4 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: Input check in Python

Posted 11 April 2011 - 05:53 AM

How about using isdigit on the entered input?
choice = 'a'
while not choice.isdigit():
    choice = input()


Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Input check in Python

Posted 11 April 2011 - 06:52 AM

I prefer Motoma's option. I'd put it in a loop with a "try again" kind of thing:
def getNum():
	while True:
		try:
			return int(input())
		except ValueError:
			print("Sorry, not number data, please try again")

choice = getNum()


This post has been edited by baavgai: 11 April 2011 - 06:52 AM

Was This Post Helpful? 1
  • +
  • -

#6 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: Input check in Python

Posted 11 April 2011 - 07:02 AM

I was thinking of avoiding the overhead of exception handling. Probably not a big deal in this case.
Was This Post Helpful? 0
  • +
  • -

#7 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: Input check in Python

Posted 12 April 2011 - 01:50 AM

Hi guys ,

Thank you all first of all . Second ,can you please explain the difference between :

choice=""
and 
choice=''




?

thanks again
Ron
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: Input check in Python

Posted 12 April 2011 - 01:56 AM

No functional difference:
http://docs.python.o...string-literals
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1