Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Suppose we want a program to request an integer (n) repeatedly until it is in

ID: 3889911 • Letter: 1

Question

1. Suppose we want a program to request an integer (n) repeatedly until it is in the range 0 to 100 inclusive.   Which of the following is the best input validation loop?

HINT: see DeMorgan’s law document.

A. while n < 0 or n > 100:

generate an error message

B. while n >= 0 and n <= 100

generate an error message

C. while n >= 0 or n <= 100

generate an error message

D. while n <= 0 or n >= 100

generate an error message

2. What sequence of values will be printed when the following instructions are executed?

      X = 5

      if (X < 7):

    print(6)

    Y = 6

else:

    print(4)

    Y = 4

if (Y < 5):

    print(3)

else:

    print(2)

3. What would be printed if the following instructions were executed?

      X = 4

      while (X > 0):

          print(X)

          X = X – 1

4. What would be printed if the following function were executed with the value of N being 9

      def xxx(N):

          if (N < 7):

              print(N)

          else:

              N = N + 3

             print(N)

Explanation / Answer

1) Correct condition should be

while ~ (n>=0 && n<=100)
Applying demorgan law ::

while n<0 or n>100

Answer is A. while n < 0 or n > 100:


2)



X = 5

if (X < 7):=> TRUE because X = 5

    print(6) =====> Prints 6 because X<7

    Y = 6=====>Y becomes 6

else:

    print(4)

    Y = 4

if (Y < 5): ==> False because Y = 6

    print(3)

else:

    print(2) ==> Finally Prints 2


Output : 6 2



3)

X = 4 ==> Initially X is 4

      while (X > 0): ===> LOOP WILL RUN UNTIL X> 0

          print(X) ==> Prints 4 , 3, 2, 1  

          X = X – 1

When X becomes 0 , loop terminates



4)


N is 9

def xxx(N):

          if (N < 7): ==>False because N = 9

              print(N)

          else:

              N = N + 3 =====> N BECOMES N +3 = 9 +3 = 12

             print(N)==> Prints 12


OUTPUT : 12


Thanks, let me know if there is any doubt