Python:打印第一个数字和第二个数字之间所有偶数的总和 [英] Python: print the sum of all the even numbers between first number and second number inclusive

查看:354
本文介绍了Python:打印第一个数字和第二个数字之间所有偶数的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好! :)



我正在尝试将代码输入输出第一个数字和第二个数字之间所有偶数之和。我必须使用 while 循环!



如果有任何建议我无法工作我真的很感激它!



我尝试过:



 < span class =code-keyword> print (  Welcome

FirstNum = int(输入( 输入您的第一个数字:))
SecondNum = int(输入( 输入你的第二个数字:))

if (FirstNum> SecondNum):
{
print 第一个数字必须小于第二个数字。
}
while {
(FirstNum< SecondNum):

i 范围内(FirstNum,SecondNum, 1 ):
while (i%2!= 0):
print (i, 为奇数
}

解决方案

嗯......你可以尝试回答这个问题,而不是回收你最后一件作业吗?


这是你的作业,所以我不会给你任何代码!

1)声明一个变量来保存总和,将其设置为零。

2)在你的循环中,检查数字是奇数还是偶数。

2.1)如果是奇数,不再做任何事情。

2.2)如果是偶数,将它加到总和上。

3)递增第一个数字,然后循环直到你的数字用完为止。

4)循环之后,打印总和。



你已经知道了如何做所有这些事情,这对你来说不应该是一个复杂的过程!


Hi there! :)

I'm trying to get the code to Output the sum of all the even numbers between first number and second number inclusive. I have to use the while loop!

I cant get it working if any has any suggestions I would really appreciate it!

What I have tried:

print("Welcome")

FirstNum=int(input("Enter your first number:"))
SecondNum=int(input("Enter your second number:"))

if (FirstNum > SecondNum):
    {
    print("First Number must be less than second number.")
    }
while{
    (FirstNum < SecondNum):

    for i in range(FirstNum, SecondNum, 1):
        while(i % 2!=0):
            print (i, "is odd")
}

解决方案

Well...you could try answering the question, instead of recycling your last piece of homework?

This is your homework, so I'll give you no code!
1) Declare a variable to hold the sum, set it to zero.
2) Inside your loop, check if the number is odd or even.
2.1) If odd, do nothing more with it.
2.2) If even, add it to the sum.
3) Increment the first number, and loop round until you run out of numbers.
4) After the loop, print the sum.

You already know how to do all of those things so this shouldn't be a complex process for you!


这篇关于Python:打印第一个数字和第二个数字之间所有偶数的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆