如果语句在for循环中结束程序

时间:2014-09-10 18:01:41

标签: if-statement for-loop python-3.x

我正在使用python 3.3.2并且我正在编写一个带有数字的程序,并按升序将其添加到列表中。该程序有效,但我添加了一个语句,检查用户输入的数字是否已经在列表中,当它为真时,循环结束并且程序停止。有人可以给我任何建议,为什么会这样,以及如何预防?

L = float(input('Input a number: ')) #Gets input from user
list1 = [L] #Places the first input in a list

for L in list1: #Begins loop
    L = input('Input a number: ') #More input

    if L == ('stop'): #User tells program to stop
         print(list1)
         break #Ends program

    elif L != ('stop'): #The user does not stop the program
        L = float(L)

        if L not in list1: #New input not in list
            N = len(list1)#Length of list
            M = max(list1)#Max value of list
            m = min(list1)#Min value of list

            if L < m: #Checks if input is smaller than the min of list1
                list1.insert(0, L)#Inserts new input at start of list

            elif L > M:#Chacks if new input is larger than the max of list1
                list1.append(L)#Add new input at ent of list1

            elif L < M and L > m:#New input is between max and min of list1

                for x in range(0 , len(list1)):#x is the number of indexes in list1

                    if L < list1[x]:#Checks the size of each index to the input
                       list1.insert(x, L)#Inserts input between upper and lower values
                       break

        elif L in list1: #Checks if new input is already in list
            print ('Already in list!')

    else: #Program fails
        print('I\'m afraid I can\'t do that Dave.')#LOL
        break

感谢。

0 个答案:

没有答案