Computer Science
Rewrite the adjacent code in python after removing all syntax error(s). Underline each correction done in the code.
30 = To
for K in range(0,To)
IF k%4 == 0:
print(K * 4)
Else:
print(K+3).
Answer
The corrected code is shown below:
To = 30 # Correction 1
for K in range(0,To): # Correction 2
if K % 4 == 0: # Correction 3
print(K * 4)
else: # Correction 4
print(K+3) # Correction 5
Explanation
Correction 1 — Variable should be on left side and literals should be on right side.
Correction 2 — Semi-colon was missing in the for loop syntax.
Correction 3 — if statement should be in lower case.
Correction 4 — else statement should be in lower case.
Correction 5 — Full stop should not be there at the end of print function.
Related Questions
What is entry controlled loop? Which loop is entry controlled loop in Python?
Explain the use of the pass statement. Illustrate it with an example.
Below are seven segments of code, each with a part coloured. Indicate the data type of each coloured part by choosing the correct type of data from the following type.
(a) int
(b) float
(c) bool
(d) str
(e) function
(f) list of int
(g) list of str(i)
if temp < 32 : print ("Freezing")
(ii)
L = ['Hiya', 'Zoya', 'Preet'] print(L[1])
(iii)
M = [] for i in range (3) : M.append(i) print(M)
(iv)
L = ['Hiya', 'Zoya', 'Preet'] n = len(L) if 'Donald' in L[1 : n] : print(L)
(v)
if n % 2 == 0 : print("Freezing")
(vi)
L = inputline.split() while L != ( ) : print(L) L = L[1 :]
(vii)
L = ['Hiya', 'Zoya', 'Preet'] print(L[0] + L[1])
Write the output of the following Python code:
for i in range(2,7,2): print(i*'$')