Question

Take a look at the code below. What is the problem with the code? Change the...

  1. Take a look at the code below. What is the problem with the code? Change the code so that it will run properly. (10 points)

i = -6

# Code the while loop

while i != 0 :

    print("Output 1")

    if i>0 :

          print("Output 2")

    else :

          print("Output 3")   

print(offset)

Homework Answers

Answer #1
Problem with the code is 
1. there is no change of value for the variable i in the loop
   So, loop runs infinitely
2. offset is not a variable
   So, out it in quotes


FIX:
i = -6
# Code the while loop
while i != 0 :
    print("Output 1")
    if i>0 :
          print("Output 2")
    else :
          print("Output 3")
    i += 1
print("offset")

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
In Java programming language 11.Create the code for a for loop to print the numbers 1  through...
In Java programming language 11.Create the code for a for loop to print the numbers 1  through 5 inclusive vertically so the output is 1 2 3 4 5 11b What is the output of the following               OUTPUT int i=3; while(int i >0) { System.out.println(i); i--; } 11c What is the output of the following               OUTPUT for(int i=3;i<10;i++) System.out.println(3*i); 11d Create the line of code to print the numbers 10 through 1 decreasing by 1 each time 11e Create the line of code...
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"...
(java) a. The following code has compilation AND logic errors. Rewrite the code (using the while...
(java) a. The following code has compilation AND logic errors. Rewrite the code (using the while loop) so it will compile correctly and print all the even numbers from 0 to 10 on one line separated by a space: int j = 0; while j > 10 ; j + 2; System.println(j, “ “); b.You want to print the values of integers a and b to an external file “test123.txt”, assigned to the PrintWriter variable output. Correct the following statements:...
IN C++ VERY EASY What's wrong with this code? The following function prints a reverse half-pyramid...
IN C++ VERY EASY What's wrong with this code? The following function prints a reverse half-pyramid populated by the alternating dots and stars (see example below). The odd rows contain stars and even rows contain dots. Debug the code to fix all the compilation and run-time errors, so that the code generates the desired output. For instance, when the 'n' value passed to the function is 6, the output would look like the following. ****** ..... **** ... ** ....
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO...
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO YOU I NEED YOUR HELP PLEASE HELP ME. I WILL GIVE UPVOTE AND GOOD COMMENT THANK YOU SO MUCH !!! QUESTION 1 What is the output after the code executes? Assume there are no syntax errors and the code will compile. int x = 10; do { System.out.println ( x ); x -= 3; } while (x > 0); A. 10 7 4 1...
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output...
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20."); else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?"); Second question Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same...
Q1: Re-write following if-else-if statements as Switch statement. Your final code should result in the same...
Q1: Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20."); else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?"); Q2: Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same...
What will be the output of the code below: for(i in 1:100) { if(i <= 20)...
What will be the output of the code below: for(i in 1:100) { if(i <= 20) { print (i) } }
I need the actual code for this... I found a similar response but it was not...
I need the actual code for this... I found a similar response but it was not thorough enough. Problem Prerequisites: None Suppose that a scientist is doing some important research work that requires her to use rabbits in her experiments. She starts out with one adult male rabbit and one adult female rabbit. At the end of each month, a pair of adult rabbits produces one pair of offspring, a male and a female. These new offspring will take one...
Convert each of the below C code snippet to LEGv8 assembly code. Assume variable a, b,...
Convert each of the below C code snippet to LEGv8 assembly code. Assume variable a, b, and c is stored in registers X19, X20, and X21 respectively. Base address of d is stored in register X22. Comment your assembly code. (24 Points) a. if (a > b) d[a] = b + 8; else d[a] = b - 16; b. for (i=0;i<a; i++) a = d[i] + c; c. i = 0; while ( d[i] == b) if(( a - i...