Why am I running into errors?

TerraceMason

I don't know why this is happening. I know it has something to do with calling 'A' because whenever 'A' is the middle value between 'C' and 'B', it cuts to else and prints "[-] Bug Detected." My code:

import time, random
run = True
while run:
    a = random.randint(1, 10000)
    b = random.randint(1, 10000)
    c = random.randint(1, 10000)
    def min3(c, b , a):
        if a == b:
            print("A is equal to B.")
        elif a == c:
            print(" A is equal to C.")
        elif b == c:
            print("B is equal to C.")
        if a > c and b > c:
            print("C is the lowest value.")
        elif a < c and b < c:
            print("C is the highest value.")
        if b > a and b > c:
            print("B is the highest value.")
        elif b < a and b < c:
            print("B is the lowest value.")
        if a > b and a > c:
            print("A is the highest value.")
        elif a < b and a < c:
            print("A is the lowest value.")
        else:
            print("[-] Bug Detected.")
            time.sleep(1)
            print(c)
            time.sleep(1)
            print(b)
            time.sleep(1)
            print(a)
            time.sleep(1)
            print("Exiting.")
            exit()
    (min3(c, b, a))
    time.sleep(1)
    print("[+] No Bug Detected.")
    time.sleep(1)
    print(c)
    time.sleep(1)
    print(b)
    time.sleep(1)
    print(a)
    time.sleep(1)

exit()

Thank you if you can help and explain to me why this is.

RightmireM

I think your bug is because of the last else. Since it hangs off the last series of if/then, it will run unless one of those conditions is met. I.e. If "A" is the middle value, it is not a > b and a > c nor is it a < b and a < c ... the else runs. The else is only associated with the 4 lines above it, not the entire run of if/thens.

That said, I don't know if you're limited to using a series of if/thens ... but this code is a little cleaner, faster, and easier...

import time, random, sys
import collections

def min3(*args):
    # Add the values to a dictionary, so you can 
    # use the VALUES passed in to get the associated letter
    d = {a:"a", b:"b", c:"c"}
    # args is a list, so all three values are in it. 
    # So, just sort it lowest to highest
    sorted_x = sorted(args)
    # Then just print them out. 
    # Use the format command for ease
    print("The lowest  is '{}' (={})".format(d[sorted_x[0]], sorted_x[0]))
    print("The middle  is '{}' (={})".format(d[sorted_x[1]], sorted_x[1]))
    print("The highest is '{}' (={})".format(d[sorted_x[2]], sorted_x[2]))
    print("Exiting.")
    # exit() # Not needed

run = True
while run:
    a = random.randint(1, 10000)
    b = random.randint(1, 10000)
    c = random.randint(1, 10000)

    (min3(c, b, a))
    #time.sleep(1)
    print("[+] No Bug Detected.")
    #time.sleep(1)
    print(c)
    #time.sleep(1)
    print(b)
    #time.sleep(1)
    print(a)
    #time.sleep(1)
    run = False # remove for infinite run

# exit() # not needed

OUTPUT:

The lowest  is 'c' (=479)
The middle  is 'a' (=7758)
The highest is 'b' (=9618)
Exiting.
[+] No Bug Detected.
479
9618
7758

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related