Rewrite the following 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)
Answers
To = 30
for K in range(0,To):
if K % 4 == 0:
print(K*4)
else:
print(K+3)
This is the corrected códe. The whole códe had mistakes let us go through them one by one.
1. 30 = To
A value is assigned to a variable but, a value cannot be assigned a variable. Variable To is assigned value 30 as shown in the corrected code.
2. for K in range(0,To)
The line should have ended with a colon (:)
3. IF k%4 == 0:
Indentation or space should have been given as shown in the corrected code above.
k and K are both different variables as Python is a case-sensitive language so, in this if statement it should have been K
IF should be in lowercase that is if
4. print (K*4)
Indentation or space should have been given as shown in the corrected code above.
5. Else:
Indentation or space should have been given as shown in the corrected code above. Else should be in lowercase that is else
6. print (K+3)
Indentation or space should have been given as shown in the corrected code above.
The output of the corrected code is
0
4
5
6
16
8
9
10
32
12
13
14
48
16
17
18
64
20
21
22
80
24
25
26
96
28
29
30
112
32
Explanation
To is a variable that is assigned value 30
for loop is running from K = 0 to K = 30
if condition is checking if K is divisible by 4 if it is getting the remainder as 0 then print() statement will print the value of K * 4 and if its not getting the remainder as 0 then else condition will print the value of K + 3
Extra Information
range() function is used to return a sequence of numbers in the order that we have set in the function.
Syntax of range() function
range(start, stop, step)
start is where we mention from which position the loop has to start. If not mentioned it starts from 0 by which we can say it is optional to mention it.
stop is where we want our loop to end. It is required to be mentioned.
step is used to increment so, that loop goes on. If not mentioned it has a default value of 1 by which we can say it is optional to mention it.
What is case sensitivity?
Case sensitivity is the term that tells us about the programming language capability of making out differences between Upper Case Letters(A, B, C, and so on) and Lower Case Letters (a, b, c, and so on).
Python is created by Guido van Rossum and came into existence in 1991. It is a high-level and general programming language. It is a very lucid programming language as it has a good language construct and object-oriented approach. It is dynamically typed and garbage-collected.
Answer:
i hope this answer in helpfull