Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
def get(count=None): if count >= 1: a = count - 1 else: a = 0 return a
一切都在标题中。只是为了运动。
谢谢
你的意思是使用三元运算符?
a = count - 1 if count >= 1 else 0
count如果是None因为您无法将非类型与整数进行比较,您的代码将失败。但我的回答是如何以“更好”的方式编写这个条件语句。
count
None
因此 - 我会写这样的函数(感谢@poke的max想法。):
max
def get(count=None): return max(count-1, 0) if isinstance(count, int) else 0