0
num = request.GET.get("num",20)

By default, the num is 20. But, if the user passes in 100, how can I set num to 50? (the max that I allow?)

3
  • 1
    What did you try? Did you an if statement yet? Commented Jan 14, 2011 at 23:27
  • 1
    If you want to trim down a number, you should use min rather than max. Commented Jan 14, 2011 at 23:30
  • 3
    If you're having trouble with max, then you're not ready to be using an ORM. (You're a bit of an enigma: this is a very novice question for someone who's asked over 800 questions...) Commented Jan 14, 2011 at 23:45

2 Answers 2

7

Just add one more line:

num = int(request.GET.get("num", 20))
if num > 50:
    num = 50

Or, if you want to use min you could write:

# "num" parameter assumed to be convertable to an `int`
num = min(int(request.GET.get("num", 20)), 50)
Sign up to request clarification or add additional context in comments.

Comments

6

Actually, you should use min rather than max:

num = int(request.GET.get("num", 20))
num = min(num, 50)

1 Comment

Funny how this is subtly broken: min("100", 50) == 50 == min("15", 50).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.