Python计算1—50之间所有奇数的和?
2个回答
展开全部
代码如下:
# Initialize a variable to store the sum
sum = 0
# Iterate through all numbers from 1 to 50
for i in range(1, 51):
# Check if the number is odd
if i % 2 != 0:
# If it is odd, add it to the sum
sum += i
# Print the sum of all odd numbers from 1 to 50
print(sum)
# Initialize a variable to store the sum
sum = 0
# Iterate through all numbers from 1 to 50
for i in range(1, 51):
# Check if the number is odd
if i % 2 != 0:
# If it is odd, add it to the sum
sum += i
# Print the sum of all odd numbers from 1 to 50
print(sum)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询