为什么我遇到错误?

时间:2019-10-26 21:11:40

标签: python

我不知道为什么会这样。我知道这与调用“ A”有关,因为无论何时“ A”是“ C”和“ B”之间的中间值,它都会切成其他字并显示“ [-]检测到错误”。 我的代码:

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()

谢谢您能帮助我,向我解释为什么。

3 个答案:

答案 0 :(得分:0)

只要a,b,c不符合前面的if语句中的条件,就在打印“ [-]检测到错误”。

else仅指第一个if语句(从其后退),因此对于您来说,a > b and a > ca < b and a < c也是如此。

因此,无论哪种情况,a都必须位于 b c的两侧。 if语句中不接受介于2之间的数字。

因此,如果a在b和c之间,则程序将打印“ [-] Bug Detected”。即b < a < cc < a < b

答案 1 :(得分:0)

def min3(c, b , a):
  maximum = max(a, b, c)

  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 == maximum:
    print("A is the highest value")
  elif b == maximum:
    print("B is the highest value")
  elif c == maximum:
    print("C is the highest value")
  else:
    print("[-] Bug Detected.")

答案 2 :(得分:0)

我认为您的错误是由于最后一个else引起的。由于它挂起了if / then的最后一个序列,因此除非满足这些条件之一,否则它将运行。即如果“ A”是中间值,则它不是a > b and a > c也不是a < b and a < c ...,否则运行。 else仅与其上方的4行相关联,而不与if / thens的整个运行相关。

也就是说,我不知道您是否仅限于使用一系列if / thens ...,但是此代码更简洁,更快,更容易...

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

输出:

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