早教吧作业答案频道 -->其他-->
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...的网友还看了以下:
热学ΔE,ΔH,q,w,ΔU,ΔE,ΔH,q,w,ΔU,怎么好像ΔE,ΔU都是内能的意思.有什么区 2020-04-27 …
连字成词(英语)l u o e b s r u s r e t o s w a e s r t e 2020-05-14 …
已知字母组合成英语单词1、e e t t i n h r 2、e e r a t w h 3、o 2020-05-14 …
A.t[1]=U[1]^T[2]=W[2]^T[3]=V[4]B.t[1]=V[1]^T[2]=U[ 2020-05-26 …
设有关系R、S和T如下所示,则元组演算表达式{t| (u)((R(u)∨S(u))∧(v)(T(v) 2020-05-26 …
A.t[1]=u[1]∧t[2]=w[2]∧t[3]=v[4]B.t[1]=v[1]∧t[2]=u[ 2020-05-26 …
A.t[1]=u[1]∧t[2]=w[2]∧t[3]=v[4]B.t[1]=v[1]∧t[2]=u[ 2020-05-26 …
matlab求解二阶导数方程,四个方程四个未知量>>symst>>E=32;G=10.81;b=2 2020-07-19 …
我们刚学这一章.我看到别人的笔记好多我都看不懂啊我抄了下来,W=U.I.T=I^2R.T(串)=U^ 2020-10-31 …
高等数学设z=uv+sint,而u=e^t,v=cost,求全导数。解:dz/dt=?为什么还要减掉 2020-11-17 …