Python社区  »  Python

条件语句打印两次python值

pyapac • 1 周前 • 15 次点击  

因此,我正在测试我的脚本,其中我传递两个值进行比较。它通过两个条件语句。我已经进行了一些调试,它输出了同一个表达式两次,即“当前值在范围内”。它首先从第一个循环打印出来,然后从第二个循环打印出来。我不知道为什么我的代码要这么做。它应该只打印一次,然后从else语句中取出,而不进入它当前正在执行的第二个else语句。我做错事是为了阻止这一切。

def compare_sizes(previous_size, current_size):

    subtract_f1_f2 = int(current_size - previous_size)
    range_num = 0.4
    range_previous_day = int(previous_size * range_num)


  if subtract_f1_f2 > 0 and range_previous_day > 0 and subtract_f1_f2 >= range_previous_day:

    whole_percent = subtract_f1_f2 / previous_size * 100
    print (human_bytes(previous_size) +" -> " + human_bytes(current_size) + " " +
                            "+" + str(whole_percent) + " % bigger" + "\n")
    return
  else:
    print("Current Value Is In Range")
  if  subtract_f1_f2 <0 and subtract_f1_f2 <= range_previous_day:
    whole_percent = abs(subtract_f1_f2 / previous_size * 100)
    print (human_bytes(previous_size) + " -> " + human_bytes(current_size) + " " + str(
            whole_percent) + " % smaller" + "\n")
  else:
    print("Current Value Is In Range")



result = compare_sizes(1000,1400)# 40% Bigger
result = compare_sizes(1000,1399)#  In Range
# result = compare_sizes(1000,599)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50692
 
15 次点击  
分享到微博