python奇数项求和_python 判断奇偶并求和,运行过程有错误,希望高手给指导一下。...

问题是:b)Addtotheconditioninthewhileloopsothatitwillstopifeithertheuserinputsazeroorthesumislargerthanlimit(iflimitisNonethenignorethesecondstoppingcondition).Donotuseab...

问题是:b) Add to the condition in the while loop so that it will stop if either the user inputs a zero or the sum is larger than limit (if limit is None then ignore the second stopping condition). Do not use a break statement to implement this feature.

底下是我写的程序,可是运行过程并没有按照描述运行,有错误:

def sum_odd_inputs(limit = None):

"""(integer) -> integer

Asks the user to enter a set of integer inputs. The function sums all odd

inputs (it discards all even inputs). The user can indicate that the set is

complete by entering the integer 0. The set will also be considered complete

if the running sum exceeds limit. If limit == None, then only a user input

of zero will complete the set.

>>>sum_odd_inputs()

Enter an integer (0 to quit): 5

Enter an integer (0 to quit): 6

Enter an integer (0 to quit): 10

Enter an integer (0 to quit): 7

Enter an integer (0 to quit): 0

12

>>>sum_odd_inputs(10)

Enter an integer (0 to quit):2

Enter an integer (0 to quit):3

Enter an integer (0 to quit):5

Enter an integer (0 to quit):1

Enter an integer (0 to quit):3

12

"""

set_sum = 0

user_input = raw_input ('Enter an integer (0 to quit):')

def even_number():

user_input%2 == 0

def odd_number():

user_input%2 != 0

while user_input%2 != 0:

if user_input != 0:

set_sum += user_input

elif set_sum <= 10:

set_sum += user_input

return set_sum

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值