[Tutor] check_range

Liam Clarke cyresse at gmail.com
Wed Dec 15 01:09:10 CET 2004


Oh, and you can return True and False without quotation marks.

 >f check_range(input):
 >    done = "True"
 >                      return int(input)

You wil also hit problems with this, unless you're using input() to
get the integer, which causes even more issues.

Apparently, input() evaluates the value returned, so someone who knows
Python could enter a Python to delete your HD...

It's recommended to get all values with raw_input(), which returns all
values as a string, hence the problem with check_range, as it's
comparing a string to a integer.

The way to make sure that an integer gets entered is as follows - 

while 1:
  try:       
    input=int(raw_input("Enter number here ")
  except TypeError:
     print "Please enter a number only"
     print
  else:
     print 'Thanks'
      print 'You entered %d' % input
     break

if check_range(input):
              done = True
              return input


That will infinitely loop until an integer is entered. raw_input gets
the string, and int() tries to convert it to an integer. Obviously you
can't convert a non-numeric character to an integer,
x=int('s') will raise an error, TypeError, hence the try/except/else
error catching clauses.

It'll look like this when running - 

Enter number here David
Please enter a number only

Enter number here Henry
Please enter a number only

Enter number here 1.0111010010101010
Thanks
You entered 1   #1.01101001 is a floating number, so int would change
'1.00101' to 1


HTH

Liam Clarke 

On Tue, 14 Dec 2004 18:46:26 -0500, Kent Johnson <kent37 at tds.net> wrote:
> You misunderstand what range() does. It returns a list of numbers starting with the lower one and up
> to but not including the upper one:
>  >>> range(5)
> [0, 1, 2, 3, 4]
>  >>> range(5, 10)
> [5, 6, 7, 8, 9]
> 
> To test for a number in a range you can use 10 < n < 90:
>  >>> x = 1
>  >>> 10 < x < 90
> False
>  >>> x = 15
>  >>> 10 < x < 90
> True
>  >>> x = 100
>  >>> 10 < x < 90
> False
> 
> Kent
> 
> 
> Marc Gartler wrote:
> > Hi all,
> >
> > I am fairly new to both Python & programming, and am attempting to
> > create a function that will test whether some user input is an integer
> > between 10 and 89, but the check isn't happening...
> >
> > def check_range(myrange):
> >     if range(myrange) != range(10,89):
> >         return "False"
> >     else:
> >         return "True"
> >
> > ...this gets called later via:
> >         if check_range(input):
> >             done = "True"
> >                 return int(input)
> >
> >
> > What am I doing wrong?
> >
> > Thanks!
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only ba sic human duty, to take the consequences.


More information about the Tutor mailing list