要用mainfunction以及whileloopdefsumDigits(number):ForexamplesumDigits(234)returns9.Usethe%operatortoextractthedigitsandthe//operatortoremovetheextracteddigit.Forexample,t...
要用main function 以及 while loop
def sumDigits(number):
For example sumDigits(234) returns 9. Use the % operator to extract the digits and the //operator to remove the extracted digit. For example, to extract 4 from 234, use 234%10(=4).To remove 4 from 234, use 234//10(=23). Use a loop to repeatedly extract and remove the digits until all the digits are extracted. Write a main function with the header def main(): that prompts the user to enter an integer, calls the sumDigits function and then displays the results returned by the function. The main function is called by writing: main().The template for the question is given below:
#Template starts here
def sumDigits(number):
#Write code to find the sum of digits in number and return the sum
def main():
#Write code to prompt the user to enter an integer, call sumDigits and display result
main()#Call to main function
#Template ends here
Sample Output
Enter a number :123
Sum of digits : 6
展开