写一个 Python示例,用 For 循环找到从 1 到 N 的偶数和奇数的和,并给出一个例子。
这个 Python示例允许用户输入最大限制值。接下来,它将打印从 1 到用户输入的极限值的偶数和奇数。在本例中,For 循环确保数字介于 1 和最大限值之间。接下来,我们使用 If Else 来检查偶数。
提示:建议大家参考偶数之和和奇数之和的文章,了解一下偶数和奇数之和背后的 For Loop 逻辑。也可以参考偶数或奇数程序和 If Else 来理解 Python 逻辑
# Python Program to find Sum of Even and Odd Numbers from 1 to N
maximum = int(input(" Please Enter the Maximum Value : "))
even_total = 0
odd_total = 0
for number in range(1, maximum + 1):
if(number % 2 == 0):
even_total = even_total + number
else:
odd_total = odd_total + number
print("The Sum of Even Numbers from 1 to {0} = {1}".format(number, even_total))
print("The Sum of Odd Numbers from 1 to {0} = {1}".format(number, odd_total))
Python 使用 For 循环输出
对偶数和奇数求和
这个 Python 奇偶数求和程序和上面的一样。但是这个 Python示例允许用户输入最小值和最大值。接下来,它打印最小值和最大值之间的偶数和奇数。
# Python Program to find Sum of Even and Odd Numbers from 1 to N
minimum = int(input(" Please Enter the Minimum Value : "))
maximum = int(input(" Please Enter the Maximum Value : "))
even_total = 0
odd_total = 0
for number in range(minimum, maximum + 1):
if(number % 2 == 0):
even_total = even_total + number
else:
odd_total = odd_total + number
print("The Sum of Even Numbers from 1 to {0} = {1}".format(number, even_total))
print("The Sum of Odd Numbers from 1 to {0} = {1}".format(number, odd_total))
Please Enter the Minimum Value : 10
Please Enter the Maximum Value : 40
The Sum of Even Numbers from 1 to 40 = 400
The Sum of Odd Numbers from 1 to 40 = 375
标签: sqlite