为什么我不能运行我的中位程序,我真的不明白

时间:2018-07-16 19:34:41

标签: python

我真的不知道我做了两个while循环,但是当我运行它时,它只显示了第一个循环。

do_caculation = True
while(do_caculation):
        while(True):
                stat_number_count = 0
                stat_number_sum = 0
                stat_number_average = 0
                stat_number_maximum = 0
                stat_number_minimum = 0
                state_number_range = 0

                try:
                    stat_number_name= input("What is the name of the file: ")
                    stat_number = open(stat_number_name,"r")
                    raw = stat_number.readlines()
                    raws = []

                    for number in raw:
                        raws.append(int(number))        

                    stat_number_count = sum(raws)
                    stat_number_sum = len(raws)
                    stat_number_maximum = max(raws)
                    stat_number_minimum = min(raws)

                    stat_number_range = stat_number_maximum - stat_number_minimum 
                    stat_number_average = stat_number_count/stat_number_sum









                except Exception as error:
                    print("An error occurred reading", stat_number_name)

                else:
                    print("File name:", stat_number_name)
                    print("Sum:", stat_number_sum)
                    print("Count:", stat_number_count)
                    print("Average:", stat_number_average)
                    print("Maximum:", stat_number_maximum)
                    print("Minimum:", stat_number_minimum)
                    print("Range:", stat_number_range)








        while(True):
                def median(raws):
                    n = len(raws)
                if n < 1:
                        print ("XD")
                if n % 2 == 1:
                        SB = sorted(raws)[n//2]
                        print (SB)
                else:
                    print (sum(sorted(raws)[n//2-1:n//2+1])/2.0

1 个答案:

答案 0 :(得分:1)

您永远不会退出第一个while循环,因此永远不会执行第二个循环。

从这段代码来看,您似乎并不真正了解while循环的作用。循环内的代码块将继续运行,直到您提供的条件不再成立为止。您正在使用

while(True)

,它永远不可能为假。因此,循环将始终运行,除非被break语句或异常打断-在这种情况下,异常不是选项,因为如果发生异常,您将捕获该异常并仅将String打印到控制台

您需要某种方法来摆脱while循环。我建议将其完全删除-在这里似乎没有必要。或者,您可以将循环的条件从True更改为最终可能为假的内容。或者,您可以在循环中放入break语句。或其他几种可能性,但是您明白我的意思。