寫一個 Python 程序,使用 For 循環、While 循環和函數,用一個實際例子來求列表中偶數和奇數的和。
Python 程序使用 For 循環查找列表中偶數和奇數的和
在這個 python 程序中,我們使用 For 循環來迭代給定列表中的每個元素。在 Python 循環中,我們使用 If 語句來檢查和查找偶數和奇數的和。
# Python Program to find Sum of Even and Odd Numbers in a List
NumList = []
Even_Sum = 0
Odd_Sum = 0
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
for j in range(Number):
if(NumList[j] % 2 == 0):
Even_Sum = Even_Sum + NumList[j]
else:
Odd_Sum = Odd_Sum + NumList[j]
print("\nThe Sum of Even Numbers in this List = ", Even_Sum)
print("The Sum of Odd Numbers in this List = ", Odd_Sum)
在這個 python 程序中,為了在一個列表中找到偶數和奇數的和,用戶輸入了項= [2,3,4,5],偶數 和= 0,奇數 和= 0。
對於循環–第一次迭代:對於範圍(0,4)
中的 0,條件為真。所以,進入 If 語句
if(NumList[0]% 2 = = 0)= > if(2% 2 = = 0)–條件為真
偶數 Sum =偶數 Sum+NumList[0]=>0+2 = 2
第二次迭代:對於範圍(0,4)中的 1–條件為真
如果(NumList[1] % 2 == 0) = >如果(3% 2 = = 0)–條件為假,則進入 Else 塊。
奇 和=奇 和+ NumList[1] = > 0 + 3 = 3
第三次迭代:對於範圍(0,4)中的 2–條件為真
如果(NumList[2] % 2 == 0) = >如果(4% 2 = = 0)–條件為真
偶數 _Sum = 2 + 4 = 6
第四次迭代:對於範圍(0,4)中的 3–條件為真
如果(5% 2 = = 0)–條件為假,則進入否則塊。
奇數 _ 總和= 3 + 5 = 8
第五次迭代:對於範圍(4)中的 4–條件為假。因此, Python 退出 For 循環
Python 程序使用 While 循環查找列表中偶數和奇數的和
這個計算偶數和奇數之和的 Python 程序同上。我們剛剛將 For Loop 替換為 While loop 。
# Python Program to find Sum of Even and Odd Numbers in a List
NumList = []
Even_Sum = 0
Odd_Sum = 0
j = 0
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
while(j < Number):
if(NumList[j] % 2 == 0):
Even_Sum = Even_Sum + NumList[j]
else:
Odd_Sum = Odd_Sum + NumList[j]
j = j+ 1
print("\nThe Sum of Even Numbers in this List = ", Even_Sum)
print("The Sum of Odd Numbers in this List = ", Odd_Sum)
使用 while 循環輸出的 Python 列表中偶數和奇數的總和
Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 22
Please enter the Value of 2 Element : 33
Please enter the Value of 3 Element : 44
Please enter the Value of 4 Element : 55
Please enter the Value of 5 Element : 99
The Sum of Even Numbers in this List = 66
The Sum of Odd Numbers in this List = 187
用函數計算列表中偶數和奇數之和的 Python 程序
這個 Python 奇數和偶數的和列表程序與第一個示例相同。但是,我們使用函數來分離邏輯
# Python Program to find Sum of Even and Odd Numbers in a List
def even_sum(NumList):
Even_Sum = 0
for j in range(Number):
if(NumList[j] % 2 == 0):
Even_Sum = Even_Sum + NumList[j]
return Even_Sum
def odd_sum(NumList):
Odd_Sum = 0
for j in range(Number):
if(NumList[j] % 2 != 0):
Odd_Sum = Odd_Sum + NumList[j]
return Odd_Sum
NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
Even_Sum = even_sum(NumList)
Odd_Sum = odd_sum(NumList)
print("\nThe Sum of Even Numbers in this List = ", Even_Sum)
print("The Sum of Odd Numbers in this List = ", Odd_Sum)
使用函數輸出的列表中偶數和奇數的總和
Please enter the Total Number of List Elements: 7
Please enter the Value of 1 Element : 12
Please enter the Value of 2 Element : 9
Please enter the Value of 3 Element : 21
Please enter the Value of 4 Element : 13
Please enter the Value of 5 Element : 87
Please enter the Value of 6 Element : 14
Please enter the Value of 7 Element : 66
The Sum of Even Numbers in this List = 92
The Sum of Odd Numbers in this List = 130
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/295404.html