Computer Science
What are the errors in following codes ? Correct the code and predict output :
def Tot(Number) #Method to find Total
Sum = 0
for C in Range (1, Number + 1):
Sum += C
RETURN Sum
print (Tot[3]) #Function Calls
print (Tot[6])
Answer
def Tot(Number): #Method to find Total
Sum = 0
for C in range(1, Number + 1):
Sum += C
return Sum
print(Tot(3)) #Function Calls
print(Tot(6))
6
21
Working
- There should be a colon (:) at the end of the function definition line to indicate the start of the function block.
- Python's built-in function for generating sequences is
range()
, not Range(). - Python keywords like
return
should be in lowercase. - When calling a function in python, the arguments passed to the function should be enclosed inside parentheses () not square brackets [].
Related Questions
What do you understand by local and global scope of variables ? How can you access a global variable inside the function, if function has a variable with same name.
What are the errors in following codes ? Correct the code and predict output :
total = 0; def sum(arg1, arg2): total = arg1 + arg2; print("Total :", total) return total; sum(10, 20); print("Total :", total)
Find and write the output of the following python code :
def Call(P = 40, Q = 20): P = P + Q Q = P - Q print(P, '@', Q) return P R = 200 S = 100 R = Call(R, S) print(R, '@', S) S = Call(S) print(R, '@', S)
Consider the following code and write the flow of execution for this. Line numbers have been given for your reference.
1. def power(b, p): 2. y = b ** p 3. return y 4. 5. def calcSquare(x): 6. a = power(x, 2) 7. return a 8. 9. n = 5 10. result = calcSquare(n) 11. print(result)