Computer Science
The code given below accepts a number as an argument and returns the reverse number. Observe the following code carefully and rewrite it after removing all syntax and logical errors. Underline all the corrections made.
define revNumber(num):
rev = 0
rem = 0
While num > 0:
rem == num % 10
rev = rev * 10 + rem
num = num//10
return rev
print(revNumber(1234))
Python Control Flow
1 Like
Answer
def revNumber(num): #correction 1
rev = 0
rem = 0
while num > 0: #correction 2
rem = num % 10 #correction 3
rev = rev * 10 + rem
num = num // 10
return rev #correction 4
print(revNumber(1234))
Explanation
- correction 1 — The correct keyword to define a function is "def" not "define".
- correction 2 — Instead of "While" (capital W) in the while loop, it should be "while" (lowercase w).
- correction 3 — The comparison operator "==" in the line rem == num % 10 should be replaced with the assignment operator "=".
- correction 4 — Moved the "return" statement outside the while loop to return the reversed number after the loop completes.
Answered By
1 Like
Related Questions
(i) Expand the following terms: POP3, URL
(ii) Give one difference between XML and HTML.
(i) Define the term bandwidth with respect to networks.
(ii) How is http different from https ?
Write a function countNow(PLACES) in Python, that takes the dictionary, PLACES as an argument and displays the names (in uppercase) of the places whose names are longer than 5 characters. For example, Consider the following dictionary:
PLACES = {1: "Delhi", 2: "London", 3: "Paris", 4: "New York", 5:"Doha"}
The output should be:
LONDON
NEW YORKWrite a function, lenWords(STRING), that takes a string as an argument and returns a tuple containing length of each word of a string. For example, if the string is "Come let us have some fun", the tuple will have (4, 3, 2, 4, 4, 3).