Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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:

Python
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")
}
Posted
Updated 10-May-18 21:31pm
v2

1 solution

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!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900