请把这个弄得更像蟒蛇?

if joyx > 50:
   joyx = 100
elif joyx < -50:
   joyx = -100
else:
   joyx = 0

最佳答案

您可以使用math.copysignabs函数编写它:

In [30]: from math import copysign

In [31]: joyx = copysign(100, joyx) if abs(joyx) > 50 else 0

例子:
In [32]: joyx = lambda x: copysign(100, x) if abs(x) > 50 else 0

In [33]: joyx(51), joyx(-51), joyx(50), joyx(-50), joyx(0)
Out[33]: (100.0, -100.0, 0, 0, 0)

10-07 20:23