简体   繁体   中英

How to print out a number if it is less than the previous number in a loop?

A problem asks for me to output the value of a number if it is less than the previous output but if it is not less than that number, skip it and continue looping until a smaller number is found and repeat until the smallest number is found. I have already made the equation work to output random results but I have trouble finding how to keep the numbers descending to the smallest value.

I have tried using if statements in a while loop but it throws an error or only outputs one value and does not continue.

import random
time = 1
while time > 0:

    f1 = random.random()
    f2 = random.random()
    f3 = random.random()
    x = 15*(f1/(f1+f2+f3))
    y = 15*(f2/(f1+f2+f3))
    z = 15*(f3/(f1+f2+f3))
    sum_fifteen = x+y+z
    from math import sqrt
    time = ((sqrt(8**2 + (x**2)))/3) + (y/5) + ((sqrt(6**2 + (z**2)))/4)
    print (time)

I want the output to be descending until there is no smaller value but it actually outputs an infinite amount of results between 6 and 7

I can't comment on your question due to low karma, but from what I read

output the value of a number if it is less than the previous output but if it is not less than that number, skip it and continue looping until a smaller number is found and repeat until the smallest number is found

And the fact is you have to stop when the smallest possible number is found. So you can consider an arbitrary range of numbers, say between 7 and 9 between which you want to generate random numbers. I don't know if has to be float or integer, but you can take a look at random.uniform(a,b) which samples a number from the uniform distribution between a and b. So if you take

number = random.uniform(6,9)

and create a loop which stops when the generated number is less than 7, which should be the smallest possible number you have taken. Printing a number lesser than the previous one should be fairly easy for you.

Below the code that you want. I clean the code that you gave and did some modification. The main problem of your infinite loop was your condition time > 0 (it was always true).

import random
from math import sqrt

# We choose arbitrarily value bigger than 7
res = 10
old_res = 11
cur_res = 10
accuracy = 0.01

while old_res-res > accuracy:
    f1 = random.random()
    f2 = random.random()
    f3 = random.random()
    x = 15*(f1/(f1+f2+f3))
    y = 15*(f2/(f1+f2+f3))
    z = 15*(f3/(f1+f2+f3))

    cur_res = ((sqrt(8**2 + (x**2)))/3) + (y/5) + ((sqrt(6**2 + (z**2)))/4)
    if cur_res < res:
        old_res = res
        res = cur_res
        print (res)

I've renamed time by res which is less confused, and I managed three variable for res. cur_res : Contain the current of the computation res : Contain the minimum old_res : Contain the last best result after res .

If you want to find the best result close to 6, you can modify the accuracy . (it's just old_res - res ). Increase the accuracy to have the better result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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