Question

In: Computer Science

Consider the following code segment:    count = 1    while count <= 10:       print(count,...

Consider the following code segment:
   count = 1
   while count <= 10:
      print(count, end=" ")
Which of the following describes the error in this code?

The loop control variable is not properly initialized.

The comparison points the wrong way.

The loop is infinite.

The loop is off by 1.

Does this code contain an error? If so, what line is the error on?

0: ans = input("Yes/No? ")
1: if ans == "Yes":
2: print("Confirmed!")
3: else
4: print("Not Confirmed!")

line 2

line 3

line 1

No error, this code is fine.

What is the output of the following code:

x = 10
  while(x>0):
      print(x-1)

9,9,9,...(forever)

9,8,7,6,5,4,3,2,1

10,9,8,7,6,5,4,3,2,1,0

9,8,7,6,5,4,3,2,1,0

In Python, how do you know that you have reached the end of the if-statement block?

A comment #

}

The code is less indented than the previous line

;

Solutions

Expert Solution

ANSWER FIRST QUESTION:-

count = 1
while count<=10:
    print(count,end=" ")

So the error in the above code is that it is an infinite loop.

What is an infinite loop?

In simple words, it is a non- terminating loop. So the statements inside the loop will keep on executing endlessly. In the above example, it will keep printing 1 on the output window. This is because the loop will keep on executing until the value of the loop is less than equal to 10. And here count is 1 and there is nothing inside the loop that is changing its value.

ANSWER SECOND QUESTION:-

0: ans = input("Yes/No? ")
1: if ans == "Yes":
2: print("Confirmed!")
3: else
4: print("Not Confirmed!")

In the above code, the error is in the third line. There is a syntax error here. unlike other programming languages like C, C++, and Java the if-else statement ends with a :(colon) in python.

So the correct code will look like this:-

ans=input("Yes/No?")
if ans=="Yes":
    print ("Confirmed")
else: #This is the error that was fixed
    print ("Not confirmed")

ANSWER TO THE THIRD QUESTION:-

x = 10
while(x>0):
    print(x-1)

It will print 9,9,9,9,9,9(Forever). Again, it is a case of an infinite loop. and x is 10 and the statement inside while i.e

print(x-1) will keep on printing 9 forever because the value of x will always remain greater than 0 as we don't have any update statement or expression inside the loop to change its value.

In Python, how do you know that you have reached the end of the if-statement block?

ANSWER- Unlike languages like C, C++, and Java Python works on indentation. So the end of the if-else will be determined by its indentation.

E.g:-

x = 5
if(x==5):
    print "Hi, I am five"
    print "Have a nice day" #end of if
x=x+1
    

So, here the indentation of the if is

print "Hi, I am five"
print "Have a nice day"

That means it will end after these lines.

The line x=x+1 is not a part of if block.

HAPPY LEARNING


Related Solutions

Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
Bash scripting return output: Determine the output this code 1. #!/bin/bash Valid = true count=1 while...
Bash scripting return output: Determine the output this code 1. #!/bin/bash Valid = true count=1 while [ $valid ] do echo $count if [ $count -eq 5 ]; then break fi ((count++)) done 2. #!/bin/bash num=100 for x in $(seq 1 5) do temp=$((num+x)) if [ $temp -1e 103]; then echo $temp else echo "Too Big" fi done 3.#!/bin/bash num=5 for x in $(seq 1 5) do echo $((num*x)) done 4. #!/bin/bash for x in $(seq 1 10) do echo...
Instructions:  Code a for statement that increments from 1 through 10, but print only the even numbers...
Instructions:  Code a for statement that increments from 1 through 10, but print only the even numbers using a fall-thru switch. You can catch the odd numbers using the option in the switch that handles everything else. Name your program ForFallThruSwitch.java. Use Java Style Guide in line advancing and spacing. --------------------OUTPUT RESULTS-------------------- Not printing odd numbers! 2 is an even number. Not printing odd numbers! 4 is an even number. Not printing odd numbers! 6 is an even number. Not printing...
Write assembly code for the following machine code. Assume that the segment is placed starting at...
Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual memory addresses represented by such labels. 0010 1010 0000 1000 0000 0000 0000 1010 0001 0001 0000 0000 0000 0000 0000 0010 0000 0010 0001 0001 1000 0000 0010 0000 0000 1000 0000 0000 0100 1110 0010 0101 0000 0010 0001 0010 1000 0000 0010 0000
a. Write machine code for the following assembly code. Assume that the segment is placed starting...
a. Write machine code for the following assembly code. Assume that the segment is placed starting at location 80000. Use decimal numbers to represent each instruction. loop:         beq $s3, $s1, endwhile                  add $t0, $s3, $s4                  lw $t1, 0($t0)                  add $s0, $s0, $t1                  addi $s3, $s3, 4                  j loop endwhile: b. Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual...
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
Fill in the blanks of the following segment of code, so that the output would be 1 3 4.
Fill in the blanks of the following segment of code, so that the output would be 1 3 4.int count = 0;do{++ count;if (count == 2)Blank;cout << count << " ";} while (count <= Blank);cout << endl;
Question 1: What does the following code print to the console when the main method is...
Question 1: What does the following code print to the console when the main method is run? public static void referenceMystery(int x, int[] a) { x = 3; a[0] = 4; System.out.println(x+" "+a[0]); } public static void main(String[] args) { int x = 1, y = 2; int[] a = new int[1]; int[] b = new int[1]; referenceMystery(y, b); } 1. x a[0] 2. 1 3. 2 0 4. 3 4 Question 2: What does the following code print out...
21. What is the output of the following segment of code if 7 is input by...
21. What is the output of the following segment of code if 7 is input by the user when asked to enter a number?____________ int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 7: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT