Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
April 28th, 2004, 04:40 AM
 winxptipster
James

Join Date: Apr 2004
Location: Beer Sheva, Israel
Posts: 12
Time spent in forums: 27 m 27 sec
Reputation Power: 0
How to ensure input number range?

I am writing my first Python program, which is a simple little lottery thing.

It runs well, but the user can input any numbers he wishes, and I want to limit him to a specific bit range.

I have tried various ways but can't seem to understand how to do this.

The problem is:
I have a seven-digit number. The first six digits can be between 1 and 35, while the seventh digit can only be between 1 and 10.

A gentle shove in the right direction would be much appreciated. Maybe even a reference for me to read? I do have the Reference Manual.

Thanks
James

#2
April 28th, 2004, 07:50 AM
 DevCoach
Contributing User

Join Date: Feb 2004
Location: London, England
Posts: 1,585
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
I presume that this is a text-mode program, in which case the best you can do is ask for a number, and if it is not in the right range then print an error message and try again.

e.g.

Code:
```def getNumberInRange(min, max):
while 1:
val = raw_input('Please enter a number between %s and %s' % (min, max) )
try:
val = int(val)
if min <= val <= max:
return val
except ValueError:
pass
print 'Incorrect - please try again'

print getNumberInRange(1, 35)```

If it is a GUI program such as Tkinter or wxWindows then you have a number of other options, such as using a spin control or a drop-down list of possible numbers.

Dave - The Developers' Coach

Last edited by DevCoach : April 28th, 2004 at 07:56 AM.

#3
April 28th, 2004, 09:21 AM
 winxptipster
James

Join Date: Apr 2004
Location: Beer Sheva, Israel
Posts: 12
Time spent in forums: 27 m 27 sec
Reputation Power: 0
Excellent, thank you very much.

Solved my immediate problem and led me into a new area (GUI). So my studies continue.

Very grateful, for sure!

James

#4
April 30th, 2004, 09:59 AM
 netytan
Hello World :)

Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
another common solusion would be to do an 'if number in range' check i.e.

Code:
```imaginary python shell
>>> min = 1
>>> max = 50
>>> num = 20
>>> if num in range(max, min): print True
True
>>>```

I also believe you can use xrange() aswell as range() for this.

Code:
```#!/usr/bin/env python

def inrange(num, min, max):
try:
return int(num) in range(max, min)
except ValueError, message:
print message

if __name__ == '__main__':

num = raw_input('Please enter a number')
min = 1
max = 50

while not inrange(num, min, max): num = raw_input('Please enter a number')```

Note: none of this is tested in particular the last section in the second example may not work however the rest is probably fine . Let me know if it doesnt work for you.

Hope this helps,

Mark.
__________________
programming language development: www.netytan.com Hula

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > How to ensure input number range?