早教吧作业答案频道 -->其他-->
python题目Writeafunctionthatcomputesandreturnsthesumofthedigitsinaninteger.Usethefollowingfunctionheader:defsumdigits(number):Forexamplesumdigits(234)returns9.Usethe%operatortoextractthedigitsandthe//operatort
题目详情
python题目
Write a function that computes and returns the sum of the digits in an integer. Use the following function header:
def sum_digits(number):
For
example sum_digits(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 sum_digits 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 sum_digits(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
Write a function that computes and returns the sum of the digits in an integer. Use the following function header:
def sum_digits(number):
For
example sum_digits(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 sum_digits 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 sum_digits(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
▼优质解答
答案和解析
#!/usr/bin/env python
#-*- coding:utf-8 -*-
def sum_digits(number):
"""Return the sum of the number's digits"""
remain = number
sumn = 0
while remain>0:
sumn += remain % 10
remain = remain // 10
return sumn
def main():
"""To interact with user"""
while True :
numberstr = raw_input("Enter a number:")
if numberstr.isdigit():
result = sum_digits(int(numberstr))
print "Sum of digits: {0}".format(result)
break
else:
print "An int type you entered is not valid!"
if __name__ == '__main__':
main()
看了 python题目Writea...的网友还看了以下:
请求翻译乱码请高手翻译下什么意思啊?亅酰谎u知欋5??k(so?笿?(n?豃]?n#?k(so? 2020-04-06 …
连字成词(英语)l u o e b s r u s r e t o s w a e s r t e 2020-05-14 …
A.O(e)B.O(e-1)C.O(e2)D.O(e+10) 2020-05-26 …
将字母组成单词,并译成汉语1.m,e,r,m,u,s,汉语2.u,l,t,e,c,u,r,汉语3. 2020-06-04 …
java答案是什么?Toaddnumbertosum,youwrite(Note:Javaisca 2020-06-06 …
请问什么时候用an?a,e,i,o,u是指单词字母吗?请问什么时候用an?一直听说遇到a,e,i, 2020-06-07 …
英语单词填空1.时间状语:d-r-n-2.场所:b-s-s-o-f-r--e-a-t-e-t3.教 2020-07-14 …
选出划线部分不同的一项1.Ahear(ear)B.pear(ear)Cthere(ere)D.whe 2020-10-29 …
英语翻译1、OldMacdonaldhadafarm.E-I-E-I-O.Andonthatfarm 2020-11-01 …
英语:下1.将下联打乱的字母组成单词,并写出汉语意思1.a,e,h,g,c,n[]2.e,i,s,t 2020-12-10 …