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

int count = 10; while (count >= 0) { cout << count << endl; count =...
int count = 10; while (count >= 0) { cout << count << endl; count = count + 3; } How many times will this loop be executed? A. Endless loop B. 3 times C. 0 times D. Once
Given the code segment: def f1(a, b = 2): if a : print(1) f1(1) what is...
Given the code segment: def f1(a, b = 2): if a : print(1) f1(1) what is the result of executing the code segment? a) Syntax error b) Runtime error c) No error d) Logic error in which scenario(s) is using a dictionary useful? a) To store the dates of lesson that each student is absent for so that warning letters may be issued to student exceeding a limit. b) To store the student numbers for students who sat for an...
Read the following code:     x = 1     while(x < 26)         print(x)         x = x + 1...
Read the following code:     x = 1     while(x < 26)         print(x)         x = x + 1 There is an error in the while loop. What should be fixed? Add a colon to the end of the statement Begin the statement with the keyword count Change the parentheses around the test condition to quotation marks Use quotation marks around the relational operator Question 2(Multiple Choice Worth 5 points) (03.03 LC) Python uses __________ to determine whether the condition of a while...
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); }
Consider the following code segment: pid_t pid; pid = fork(); if (pid == 0) { /*...
Consider the following code segment: pid_t pid; pid = fork(); if (pid == 0) { /* child process */ pthread_create( ... ); fork(); } pthread_create( ... ); fork(); the remaining code (not shown) does not call fork() nor call pthread_create(). Assume each invocation to fork() or pthread_create() was successful. a. How many unique processes are created (including the first main or root process)? Justify your answer. b. How many unique threads are created by pthread_create()? Justify your answer.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT