问题是: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
展开