创新互联python教程:
成都创新互联公司是专业的潞州网站建设公司,潞州接单;提供成都做网站、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行潞州网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
写一个 Python 程序来求一个集合中偶数和奇数的和。if 条件(if(eoVal % 2 == 0))检查可被二整除的集合项是否等于零。如果为真,则将该设置值添加到 sEvenSum 否则,添加到 sOddSum。
# Sum of Set Even and Odd Numbers
evenoddSet = {78, 64, 11, 95, 36, 66, 77, 151}
print("Even and Odd Set Items = ", evenoddSet)
sEvenSum = sOddSum = 0
for eoVal in evenoddSet:
if(eoVal % 2 == 0):
sEvenSum = sEvenSum + eoVal
else:
sOddSum = sOddSum + eoVal
print("The Sum of Even Numbers in evenoddSet = ", sEvenSum)
print("The Sum of Odd Numbers in evenoddSet = ", sOddSum)
这个 Python 奇偶和示例允许输入集合项。
# Sum of Set Even and Odd Numbers
evenoddSet = set()
number = int(input("Enter the Total Even Odd Set Items = "))
for i in range(1, number + 1):
value = int(input("Enter the %d Set Item = " %i))
evenoddSet.add(value)
print("Even and Odd Set Items = ", evenoddSet)
sEvenSum = sOddSum = 0
for eoVal in evenoddSet:
if(eoVal % 2 == 0):
sEvenSum = sEvenSum + eoVal
else:
sOddSum = sOddSum + eoVal
print("The Sum of Even Numbers in evenoddSet = ", sEvenSum)
print("The Sum of Odd Numbers in evenoddSet = ", sOddSum)
Python 集合输出中偶数和奇数的和
Enter the Total Even Odd Set Items = 7
Enter the 1 Set Item = 22
Enter the 2 Set Item = 44
Enter the 3 Set Item = 87
Enter the 4 Set Item = 99
Enter the 5 Set Item = 122
Enter the 6 Set Item = 321
Enter the 7 Set Item = 439
Even and Odd Set Items = {321, 439, 99, 44, 22, 87, 122}
The Sum of Even Numbers in evenoddSet = 188
The Sum of Odd Numbers in evenoddSet = 946
在这个 Python 集合的例子中,我们创建了一个 sumOfSetEvenandOddNumbers 函数,该函数返回一个集合中偶数和奇数的总和。
# Sum of Set Even and Odd Numbers
def sumOfSetEvenandOddNumbers(evenoddSet):
sEvenSum = sOddSum = 0
for eoVal in evenoddSet:
if(eoVal % 2 == 0):
sEvenSum = sEvenSum + eoVal
else:
sOddSum = sOddSum + eoVal
return sEvenSum, sOddSum
evenoddSet = set()
number = int(input("Enter the Total Even Odd Set Items = "))
for i in range(1, number + 1):
value = int(input("Enter the %d Set Item = " %i))
evenoddSet.add(value)
print("Even and Odd Set Items = ", evenoddSet)
sESum, sOSum = sumOfSetEvenandOddNumbers(evenoddSet)
print("The Sum of Even Numbers in evenoddSet = ", sESum)
print("The Sum of Odd Numbers in evenoddSet = ", sOSum)
Enter the Total Even Odd Set Items = 5
Enter the 1 Set Item = 12
Enter the 2 Set Item = 33
Enter the 3 Set Item = 44
Enter the 4 Set Item = 86
Enter the 5 Set Item = 99
Even and Odd Set Items = {33, 99, 12, 44, 86}
The Sum of Even Numbers in evenoddSet = 142
The Sum of Odd Numbers in evenoddSet = 132
文章题目:Python 程序:求集合中偶数和奇数的和
文章起源:http://www.mswzjz.cn/qtweb/news30/56530.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能