convert the while loop into for loop
x = int(input('Enter initial value: '))
count = 0 if(x<1):
print('Error')
exit()
print('Initial value is: ',x,' ',end='')
while(x!=1):
if(x%2==0):
x=x/2
else:
x=3*x+1
count+=1
if(x!=1):
rint('Next value is: ',int(x),' ',end='')
else: print('Final value ',int(x),', ',end='')
print('number of operations performed ',int(count),' ',end='')
break
x = int(input('Enter initial value: ')) count = 0 if x < 1: print('Error') exit() print('Initial value is: ', x, ' ', end='') # this does not have number of iterations already defined. # so, it's never a good idea to use a for loop in this scenario # anyway I am going to change this to a for loop for i in range(100000): if x % 2 == 0: x = x / 2 else: x = 3 * x + 1 count += 1 if x != 1: print('Next value is: ', int(x), ' ', end='') else: print('Final value ', int(x), ', ', end='') print('number of operations performed ', int(count), ' ', end='') break
Get Answers For Free
Most questions answered within 1 hours.