编写一个 Python 程序,使用 For Loop、While Loop 和函数来计算列表中的正数和负数,并附带实际示例。
Python 程序:使用 For Loop 计算列表中的正数和负数
在此 Python 程序中,我们使用 For Loop 来迭代给定列表中的每个元素。接下来,在 for 循环内,我们使用 If 语句来检查和计算正数和负数。
# Example
NumList = []
Positive_count = 0
Negative_count = 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] >= 0):
Positive_count = Positive_count + 1
else:
Negative_count = Negative_count + 1
print("\nTotal Number of Positive Numbers in this List = ", Positive_count)
print("Total Number of Negative Numbers in this List = ", Negative_count)

在此Python程序中,用户输入的列表元素 = [12, -22, 3, 5],Positive_count = 0,Negative_count = 0。
For循环 – 第一次迭代:for 0 in range(0, 4)
条件为 True。因此,它进入If 语句
if(NumList[0] >= 0) => if(12 >= 0) – 条件为 True
Positive_count = Positive_count + 1 => 0 + 1 = 1
第二次迭代:for 1 in range(0, 4) – 条件为True
if(NumList[1] >= 0) => if(-22 >= 0) – 条件为 False,因此进入 Else 块。
Negative_count = Negative_count + 1 => 0 + 1 = 1
第三次迭代:for 2 in range(0, 4) – 条件为True
if(NumList[2] >= 0) => if(3 >= 0) – 条件为 True
Positive_count = 1 + 1 => 2
第四次迭代:for 3 in range(0, 4) – 条件为True
if(5 >= 0) – 条件为 True。因此,进入 Else 块。
Positive_count = 2 + 1 => 3
第五次迭代:for 4 in range(4) – 条件为 False。因此,Python 退出 For Loop
Python 程序:使用 While 循环计算列表中的正数和负数
此计算正数和负数的程序与上面相同。我们只是将For Loop替换为While 循环。
NumList = []
Positive_count = 0
Negative_count = 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] >= 0):
Positive_count = Positive_count + 1
else:
Negative_count = Negative_count + 1
j = j + 1
print("\nTotal Number of Positive Numbers in this List = ", Positive_count)
print("Total Number of Negative Numbers in this List = ", Negative_count)
使用 while 循环计算列表中的正数和负数,输出如下所示。
Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : -3
Please enter the Value of 2 Element : -5
Please enter the Value of 3 Element : 9
Please enter the Value of 4 Element : 8
Please enter the Value of 5 Element : -6
Total Number of Positive Numbers in this List = 2
Total Number of Negative Numbers in this List = 3
Python 程序:使用函数计算列表中的正数和负数项
此计算正数和负数列表项的程序与第一个示例相同。但是,我们使用函数将逻辑分开了。
def count_Positive(NumList):
Positive_count = 0
for j in range(Number):
if(NumList[j] >= 0):
Positive_count = Positive_count + 1
return Positive_count
def count_Negative(NumList):
Negative_count = 0
for j in range(Number):
if(NumList[j] % 2 != 0):
Negative_count = Negative_count + 1
return Negative_count
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)
Positive_cnt = count_Positive(NumList)
Negative_cnt = count_Negative(NumList)
print("\nTotal Number of Positive Numbers in this List = ", Positive_cnt)
print("Total Number of Negative Numbers in this List = ", Negative_cnt)
Please enter the Total Number of List Elements: 6
Please enter the Value of 1 Element : -11
Please enter the Value of 2 Element : -22
Please enter the Value of 3 Element : 33
Please enter the Value of 4 Element : 44
Please enter the Value of 5 Element : -55
Please enter the Value of 6 Element : 66
Total Number of Positive Numbers in this List = 3
Total Number of Negative Numbers in this List = 3