tags:

views:

67

answers:

4

#hello , i wounder why my code keep stoping at the secound while loop and doesn't do anything

print"*******************************"
a = 0
deg_list =[]
deg_list_a=[]
deg_list_b=[]
deg_list_c=[]
degree=input("Enter the students Degree:")
while a<=degree:
    deg_list.append(degree);
    degree=input("Enter the students Degree:")
print "Degree List :",deg_list
print len(deg_list)
while len(deg_list)>=0:
    if deg_list[a]>=16:
        deg_list_a.append(deg_list[a])
        x=+1
    elif 15>deg_list[a]>=10:
        deg_list_b.append(deg_list[a])
        x=+1
    else :
        deg_list_b.append(deg_list[a])
        x=+1
print deg_list_a
print deg_list_b
print deg_list_c
+3  A: 

Your code enters an endless loop.

Both of your while loops have problems with the condition which allows them to terminate. Since your code never changes the value of a, the first loop becomes while 0<=degree, and so the first loop terminates when the user inputs a negative value. But the variable a can be removed from your program.

The while loop continues as long as len(deg_list) >= 0. However, no code within the loop decreases the length of deg_list, so the while loop continues forever.

The code below could help you get this working:

deg_list =[]
deg_list_a=[]
deg_list_b=[]
deg_list_c=[]
degree=input("Enter the students Degree:")
while degree > 0:
    deg_list.append(degree);
    degree=input("Enter the students Degree:")
print len(deg_list)
while len(deg_list) > 0:        # Strictly greater than 0, not equal to 0.
    if deg_list[0] >= 16:
        # Use pop to access first element
        deg_list_a.append(deg_list.pop(0))    
    elif deg_list[0] >= 10:    # One comparison per statement, only.
        deg_list_b.append(deg_list.pop(0))
    else:
        deg_list_c.append(deg_list.pop(0))  # c, not b.
print deg_list_a
print deg_list_b
print deg_list_c           
Heath Hunnicutt
pop() for the first element? Only if you pass 0 as the index to pop(), otherwise it defaults to -1, which is the last element. - [linkage](http://docs.python.org/library/stdtypes.html#mutable-sequence-types)
nilamo
i tested ur code and it has the same problem with while it ends at print len(deg_list)
Fiasco
@linkage, thanks.
Heath Hunnicutt
@Fiasco, it works for me, but you will need to add the `print deg_list_a` statement from your original code. My version didn't print the output. I will change it for you.
Heath Hunnicutt
@Heath Hunnicutt, Oh thank you for your help
Fiasco
@Fiasco, You are welcome. Once you have your program working, choose your favorite answer here and click the "check mark" outline to the left of it. This finishes the question.
Heath Hunnicutt
+1  A: 

You're never modifying deg_list, so your loop becomes infinite. Even removing all elements wouldn't help since you're comparing against 0 -- the loop condition will never be false.

Daenyth
+1  A: 

Well.

It looks to me that a is set to 0 in the beginning and then never changed, so doing something with deg_list[a], that is the first element in the list, isn't going to do very much. In addition, your looping condition is len(deg_list) >= 0, and len(deg_list) will never change.

But there are more fundamental issues with your code. Imagine you were changing the length of deg_list: in this case you would be changing the very list you're looping over, which is usually (if you are not very very certain what you're doing) a recipe for disaster. What I think you should envisage doing is a loop along the lines of:

for degree in deg_list:
    if [degree fulfils some condition]:
        [do something with degree]
    elif [degree fulfils some other condition]:
        [do something else]
    ...
    else:
        [whatever]

Last, from your comparison it seems that the "degrees" are all small integers. You may want to test for that -- it's user input and you have to expect anything being thrown at your input -- before doing things like if degree >= 16.

chryss
A: 

It looks like you are trying to loop through all the members of deg_list, but you are waiting for deg_list to be empty, and each time through the loop you are incrementing "x," which is never even read.

If you really are trying to traverse through deg_list, try this for your second loop:

for degree in deg_list:
    if degree >= 16:
        deg_list_a.append(degree)
    elif degree >= 10:
        deg_list_b.append(degree)
    else :
        deg_list_c.append(degree)
sirconnorstack