Question

convert the while loop into for loop x = int(input('Enter initial value: ')) count = 0...

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

Homework Answers

Answer #1
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
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
c++ Convert the following for loop to a while loop: for (int x = 50; x...
c++ Convert the following for loop to a while loop: for (int x = 50; x > 0; x-=2) {      cout << x << " seconds to go.\n"; }
3.plese to write the correct code: The code below will loop as long as the number...
3.plese to write the correct code: The code below will loop as long as the number that you enter isn’t negative. It will add each non negative number to the current sum. It will print out the sum and average of all of the numbers you have entered. But someone jumbled up the code. Can you reorganize and indent it so it works properly? count = count + 1 message = "Enter an integer or a negative number to stop"...
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
8.36 Lab 8a: Count by 3 Introduction For this lab, you will use a while loop...
8.36 Lab 8a: Count by 3 Introduction For this lab, you will use a while loop to complete the task. A while loop has this basic structure: /* variable initializations */ while (/*stop condition*/){ /* statements to be performed multiple times */ /* make sure the variable that the stop condition relies on is changed inside the loop. */ } Despite the structure of the while loop being different than that of a for loop, the concept behind it is...
(c programming) Examine the following code: int x = 5; while(x = 0){ x += x;...
(c programming) Examine the following code: int x = 5; while(x = 0){ x += x; } while(x-- > 5){} What is the final value of x? Does the assignment expression in the first loop occur and if so, what value does it evaluate to?
Turning this into C++ start Declarations int nums[3] for (int =0; i<3, i++) output "Enter number"...
Turning this into C++ start Declarations int nums[3] for (int =0; i<3, i++) output "Enter number" input value output "Enter number" input value output "Enter number" input value end
CODE BLOCK D flag = 0 while flag == 0 : guess = input ("Select an...
CODE BLOCK D flag = 0 while flag == 0 : guess = input ("Select an option:\n1. Add a course\n2. Drop a course\n3. Print your registration.\n4. Quit\n\n>>> ") if guess.isdigit() : guess = int(guess) if guess == 1: print("Lets add a course") elif guess == 2: print("Lets drop a course") elif guess == 3: print("Lets print your registration") elif guess == 4: print("Bye") flag = 1 else: print(“Huh?”) If the user enters the value 5. What will print?
For coding below how do i loop every letter of the input of the password. When...
For coding below how do i loop every letter of the input of the password. When my first letter is a number it detects as passowrd with numeric but if i put the number in the end of the word its doesnt detect password with numeric password=input("Enter your Password: ") if (len(password)<6): print("Must be at least 6 characters long") for i in password: if i.isnumeric== True: print("All good characters") else: print("Your password must contain at lease 1 number") break