Another python question

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Fri Mar 18 18:59:57 EDT 2016


On Friday, March 18, 2016 at 3:46:44 PM UTC-7, Alan Gabriel wrote:
> Sorry for the multiple questions but my while loop is not working as intended.
> 
> Here is the code :
> n = 1
> list1 = []
> count = 0  #amount of times program repeats
> steps = 0 # amount of steps to reach 1
> step_list = []
> while n!=0:
>     n= int(input())
>     list1.append(n)
> length = len(list1)
> 
> while count < length:
>     num= list1[count]
>     while num!= 1:
>         if num%2 == 0:
>             num= int(num/2)
>             print(num)
>             steps+=1
>         if num%2 == 1:
>             num=int(3*num+1)
>             print(num)
>             steps+=1
>             
>     count+=1
>     step_list.append(steps)
>     steps=0
> 
> This code is meant to get numbers from the user in different lines and convert into a list. When this is done the first while loop  runs until it checks all the numbers in the list. The 2nd while loop is meant to stop when the number is equal to 1 however the 2nd while loop keeps running.
> 
> Sorry for the bad naming but I was in a rush, ty anyway

I'll give you a big hint.  Your problem is on this line:

        if num%2 == 1:

Step through your program slowly.  What happens when num == 2?



More information about the Python-list mailing list