我试图确保值tDiscriminant始终为正。如下所示,它是根据三个随机生成的数字计算得出的。

s = random.uniform(1,100)
sRounded = round(s,1)
v = random.uniform(1,50)
vRounded = round(v,1)
a = random.uniform(1,20)
aRounded = round(a,1)
tDiscriminant = (vRounded*vRounded)-(2*aRounded*sRounded)
tDiscriminantRooted = math.sqrt(tDiscriminant)


如何使tDiscriminant为负数,必须再次生成值sva,直到tDiscriminant为正数?

最佳答案

尝试

import random
tDiscriminant = -1
while tDiscriminant <= 0:
  s = random.uniform(1,400)
  sRounded = round(s,1)
  v = random.uniform(1,50)
  vRounded = round(v,1)
  a = random.uniform(1,20)
  aRounded = round(a,1)
  tDiscriminant = (vRounded*vRounded)-(2*aRounded*sRounded)
tDiscriminantRooted = math.sqrt(tDiscriminant)

10-06 01:54