python 编程问题
Writeafunctionsum_from_to(start,end)thatusesawhilelooptocomputethesumoftheintegersfro...
Write a function sum_from_to(start, end) that uses a while loop to compute the sum of the integers from start up to, but not including end. Don't forget to initialize!
Examples:
sum_from_to(3, 7) should evaluate to 18 (i.e. 3+4+5+6).
sum_from_to(3, 3) should evaluate to 0.
要求用while语句写一个方程 展开
Examples:
sum_from_to(3, 7) should evaluate to 18 (i.e. 3+4+5+6).
sum_from_to(3, 3) should evaluate to 0.
要求用while语句写一个方程 展开
展开全部
a=int(raw_input("请输入起始数字:"))
print(a)
b=int(raw_input("请输入结束数字:"))
print (b)
num=0
for i in range(a,b):
num=num+i
print ("计算结果为:")
print (num)
不好意思,我刚才用for写的,测试结果正确。你稍微改一下就能实现while循环。
print(a)
b=int(raw_input("请输入结束数字:"))
print (b)
num=0
for i in range(a,b):
num=num+i
print ("计算结果为:")
print (num)
不好意思,我刚才用for写的,测试结果正确。你稍微改一下就能实现while循环。
来自:求助得到的回答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接:
>>> def sum_from_to(start, end):
return sum(range(start,end))
运行一下,结果为:
>>> sum_from_to(3,7)
18
>>> sum_from_to(3,3)
0
>>> def sum_from_to(start, end):
return sum(range(start,end))
运行一下,结果为:
>>> sum_from_to(3,7)
18
>>> sum_from_to(3,3)
0
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
def sum_from_to(start, end):
if start > end:
print 'start must leter than end'
else:
i = 0
j = end - start
if j == 0:
return 0
else:
while i < j:
start = start + i
i += 1
return start
if start > end:
print 'start must leter than end'
else:
i = 0
j = end - start
if j == 0:
return 0
else:
while i < j:
start = start + i
i += 1
return start
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
def sum_from_to(start, end):
sum = 0
while start < end:
sum += start
start += 1
return sum
sum = 0
while start < end:
sum += start
start += 1
return sum
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询