In your Python code you're not using d
when you calculate k
, you're calculating a different random number.
In Python you can't concatenate strings with numbers. JS is using that to convert the number to a string, in Python you do that with the str()
function.
The translation should be:
d = random.random()
k = int(1e3 * d) / 2
s = str(d)
In Python 3.8 you could use the "walrus" operator to assign to d
in a one-liner
k, s = int(1e3 * (d := random.random())) / 2, str(d)
But this is confusing, so I don't recommend it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…