Rewrite the following code in python afterremoving all syntax error(s). Underline each correction done in the code. Num = input("Number:") Sum = 0 for i in range(10,Num,3) Sum+=i if i%2=0: print ( i*2) Else: print ( i*3 print Sum)
Answers
Answered by
23
Given code:
Num = input("Number: ")
Sum = 0
for i in range(10, Num, 3):
Sum += i
if i%2 = 0:
print(i*2)
Else:
print(i*3 print Sum)
Corrected code:
Num = int(input("Number: "))
Sum = 0
for i in range(10, Num, 3):
Sum += i
if i%2 == 0:
print(i*2)
else:
print(i*3)
print(Sum)
Explanations:
1. Num = input("Number: ")
- Since the programmer is expecting an integer value to be input, the int() data type must also be given along with input().
2. if i%2 = 0:
- When testing for equality, double equal to (==) symbols must be there.
3. Else:
- Uppercase 'E' isn't the right syntax in an if-else clause.
4. print (i*3 print Sum)
- The programmer must give both print statements separately and not in one pair of brackets.
- The programmer must also ensure that he puts the second print statement outside the for loop, else, it would keep printing the sum the number of times given in the range.
Similar questions