python奇偶求和_python 判断奇偶并求和,运行过程有错误,希望高手给指导一下。...
問(wèn)題是: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.
底下是我寫的程序,可是運(yùn)行過(guò)程并沒(méi)有按照描述運(yùn)行,有錯(cuò)誤:
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
總結(jié)
以上是生活随笔為你收集整理的python奇偶求和_python 判断奇偶并求和,运行过程有错误,希望高手给指导一下。...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: cdn可以防止ddos(cdn如何防止d
- 下一篇: (linux对通信)