简体   繁体   English

如何在Python中使用max函数?

[英]How do I use the max function in Python?

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? 默认情况下,num是20。但是,如果用户输入100,我如何将num设置为50? (the max that I allow?) (我允许的最大值?)

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: 或者,如果您想使用min ,则可以编写:

# "num" parameter assumed to be convertable to an `int`
num = min(int(request.GET.get("num", 20)), 50)

Actually, you should use min rather than max : 实际上,您应该使用min而不是max

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM