0% found this document useful (0 votes)
111 views14 pages

02 Odd or Even

Uploaded by

temp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views14 pages

02 Odd or Even

Uploaded by

temp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

9/22/21, 11:53 PM 02 Odd Or Even

PRACTICE PYTHON
Beginner Python exercises Follow @practice_python

Home
Why Practice Python?
Why Chilis?
Resources for learners
Exercises
Blog
About

05 FEBRUARY 2014

Odd Or Even
input
if
types
int
equality
comparison
numbers
mod

Again, the exercise comes first (with a few extras if you want the extra challenge or want to spend
more time), followed by a discussion. Enjoy!

Exercise 2 (and Solution)


Ask the user for a number. Depending on whether the number is even or odd, print out an appropriate
message to the user. Hint: how does an even / odd number react differently when divided by 2?

Extras:

1. If the number is a multiple of 4, print out a different message.


2. Ask the user for two numbers: one number to check (call it num) and one number to divide by
(check). If check divides evenly into num, tell that to the user. If not, print a different appropriate
message.

Discussion
Concepts for this week:

Modular arithmetic (the modulus operator)


Conditionals (if statements)
Checking equality

Modular arithmetic (the modulus operator)


We have been doing arithmetic (addition, subtraction, multiplication, division) since elementary
school, and often it is useful for us to find not the answer to a division problem but the remainder
when we do a division operation. This operation is called the “modulus operation.” For example,
when I divide 5 by 3, the remainder is 2, and the sentence reads like this: “5 modulo 3 is 2.”

In the Python shell:

>>> 5 % 3

>>> 6 % 3

https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 1/14
9/22/21, 11:53 PM 02 Odd Or Even

>>> 7 % 3

The % sign is exactly the modulus operator.

Conditionals
When a computer (or a program) needs to decide something, it checks whether some condition is
satisfied, which is where the term conditional comes from. Conditionals are a fancy way of saying “if
statements”. If Michele was born in New York, she has an American passport. That statement is a
conditional (if statement) that in this case is true. In Python this works the same way:

if age > 17:

print("can see a rated R movie")

elif age < 17 and age > 12:

print("can see a rated PG-13 movie")

else:

print("can only see rated PG movies")

When the program gets to the if statement, it will check the value of the variable called age against all
of the conditions, in order, and will print something to the screen accordingly. Note that elif is a
portmanteau of “else” and “if”. So if the variable age holds the value 15, the statement "can see a rated
PG-13 movie" will be printed to the screen.

Note how the statement elif age < 17 and age > 12 has the statement and - you can use or and not in
the same way. Understanding a bit about logic and how it works, or being able to rationally think
about logic will help you get the conditions right - oh, and a lot of practice.

Links about conditionals:

The official Python documentation


Python for beginners explains conditionals

Checking for equality (and comparators in general)


A fundamental thing you want to do with your program is check whether some number is equal to
another. Say the user tells you how many questions they answered incorrectly on a practice exam, and
depending on the number of correctly-answered questions, you can suggest a specific course of action.
For integers, strings, floats, and many other variable types, this is done with a simple syntax: ==. To
explicitly check inequality, use !=.

if a == 3:

print("the variable has the value 3")

elif a != 3:

print("the variable does not have the value 3")

https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 2/14
9/22/21, 11:53 PM 02 Odd Or Even

Notice how in this example, the condition is redundant. In the first condition we are checking whether
the variable a has the value 3 and in the second, we are checking whether a does NOT have the value
3. However, if the first condition is not true (a is in fact not 3), then the second condition is by
definition true. So a more efficient way to write the above conditional is like this:

if a == 3:

print("the variable has the value 3")

else:

print("the variable does not have the value 3")

The same equality / inequality comparisons work for strings.

Links about equality and comparators:

The official Python documentation

Happy coding!
Forgot how to submit exercises?

Share the fun!

« Previous exercise Next exercise »

https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 3/14
9/22/21, 11:53 PM 02 Odd Or Even

ALSO ON PRACTICEPYTHON

4 years ago • 890 comments 4 years ago • 332 comments 4 yea


03 List Less
05 List Overlap Than Ten 24
Solutions Solutions Ga

https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 4/14
9/22/21, 11:53 PM 02 Odd Or Even

Sponsored

Berlin Hotels Might be Cheaper Than You Think


Berlin Apartments | Search Ads

Beirut: Online Jobs in the USA May Pay More Than You Think
Jobs in the USA | Search Ads

If Your Cat Often Head Bumps You, Here's What It Means


TheSeniorMag.com

Hair Loss Treatment Costs in Beirut Might Actually Surprise You


Hair Loss Treatment | Search Ads

Tiger Wood's Ex-wife Turns 40, Hold Your Heart Before You See Her
WTFacts

How Meghan Markle Looks Without Makeup Is Tough To Handle


Doctor Report

Comments Community 🔒 Privacy Policy 


1 Login

 Recommend 8 t Tweet f Share Sort by Oldest

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Luciano Alves • 3 years ago


https://gist.github.com/luc...
1△ ▽ • Reply • Share ›

nikhil kutewalla • 3 years ago


number = int(input("Enter your number : "))

if(number % 2 == 0):

if(number % 4 ==0 ):

print("number is even and divisible by 4")

else:

print("number is even")

else:

https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 5/14
9/22/21, 11:53 PM 02 Odd Or Even
print("number is odd")

print("")

print("")

number1 = int(input("Enter your number1 : "))


number2 = int(input("Enter your number2 : "))
if(number1 % number2 == 0):

print(str(number1)+" is evenly divisible by


"+str(number2))

else:

print(str(number1)+" is not evenly divisible by


"+str(number2))
4△ ▽ • Reply • Share ›

Maxamed Đr Sahal > nikhil kutewalla


• 3 years ago
what does that % mean in python plz ?

i am learning python through this mini projects and


challenges
1△ ▽ • Reply • Share ›

David Mbwana > Maxamed Đr Sahal


• 3 years ago
It means modulus or remainder. for example
5 divided by 2 is 2 remainder 1
△ ▽ • Reply • Share ›

Alex Jung > nikhil kutewalla • 2 years ago


Do you have to nest the if-statements? I just did it
like this:

number = int(input("Number: "))

if number%2 == 0 and number%4 != 0:


print "Your number is even..."

elif number%4 == 0:

print "Your number is a multiple of 4"

else:

print "Your number is odd..."


△ ▽ • Reply • Share ›

Bob > Alex Jung • 2 months ago


or you can change the order:

if number % 4 == 0:

something

if number % 2 == 0:

something

this way will check first if the number is


divisible by 4
△ ▽ • Reply • Share ›

Monisha Moni > nikhil kutewalla • a year ago


Instead of printing it as str(number1) , cant we use
format specifiers? i.e

print("{} is evenly divisible by


https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 6/14
9/22/21, 11:53 PM 02 Odd Or Even

{}".format(number1,number2))
△ ▽ • Reply • Share ›

bharath > nikhil kutewalla • 3 months ago


your code having soo many errors like
indentation....

i modified your code...

check it..

code:

number = int(input("Enter your number : "))

if number % 2 == 0:

if number % 4 ==0 :

print("number is even and divisible by 4")

else:

print("number is even")

else:

print("number is odd")

print("")

print("")

number1 = int(input("Enter your number1 : "))

number2 = int(input("Enter your number2 : "))

if number1 % number2 == 0:

print(str(number1)+" is evenly divisible by


"+str(number2))

else:

print(str(number1)+" is not evenly divisible by


"+str(number2))
△ ▽ • Reply • Share ›

rybczynski • 3 years ago


https://gist.github.com/ryb...
△ ▽ • Reply • Share ›

Adamwp1993 • 3 years ago


https://gist.github.com/you...
△ ▽ • Reply • Share ›

Webber • 3 years ago • edited


inp = int(input("Give me a number\n"))

nType = "odd"

msg = ""

if inp % 4 == 0:

nType = "even"

msg = "You gave me {} which is {} & divisible by 4!\n


elif inp % 2 == 0:

nType = "even"

if msg == "": msg = "You gave me {} which is {}\n".format


print(msg)

num = int(input("Okay so give me another number\n"))

check = int(input("One more number please\n"))

if num % check == 0:

print("{} is divisible by {}!".format(num,check))

else:

print("{} is not divisible by {}!".format(num,check))

R l Sh
https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 7/14
9/22/21, 11:53 PM 02 Odd Or Even
1△ ▽ • Reply • Share ›

Victoria > Webber • 3 years ago • edited


I am just curious to know why you end your print
lines with the \n newline function?
△ ▽ • Reply • Share ›

Webber > Victoria • 3 years ago


Force of habit to be honest, I tend to use it a
lot with the inp() function as I prefer the
extra line, then I just stuck to doing it
unnecessarily in my print functions.

Bit of a bad habit that I should stop to be


honest
2△ ▽ • Reply • Share ›

Victoria > Webber • 3 years ago


I was mostly wondering if you might
know something I didn't! lol. I don't
merge my print with my input()
functions, so I get the extra line by
default. But I'm still so new to this,
that I do everything the "long way." I
would like to learn some shortcuts!
To each their own!
1△ ▽ • Reply • Share ›

Webber > Victoria • 3 years ago


I'm in the same boat! Quite new,
trying to learn new ways of doing
things. I've been really enjoying these
exercises for those reasons I guess.
Have you posted your work by any
chance?
△ ▽ • Reply • Share ›

Victoria > Webber • 3 years ago


I haven't posted it. I should create a
github. I have it all saved. I can PM it
to you, if you'd like to see it?
△ ▽ • Reply • Share ›

Webber > Victoria • 3 years ago


Yeah that'd be cool, I'm tempted to
set up a github but I've been putting
it off for a while.
△ ▽ • Reply • Share ›

Victoria > Webber • 3 years ago


I finally set up a GitHub. I'll slowly
start posting more stuff on there, a
little bit at a time, but for now, you
h t I' d f t
https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 8/14
9/22/21, 11:53 PM 02 Odd Or Even
can see what I've done so far at:

https://gist.github.com/vka...
△ ▽ • Reply • Share ›

Webber > Victoria • 3 years ago


Ahh I didn't think of a /0 error trap,
smart!
△ ▽ • Reply • Share ›

Webber > Victoria • 3 years ago


Hey! How are you getting on with
GitHub? I keep meaning to try it out
△ ▽ • Reply • Share ›

Webber > Victoria • 3 years ago


Hey! 5 Months later I've set up a
GitHub!

https://github.com/Webbarrr/

I haven't put any Python up yet, just


VB.NET / C# stuff but I'll get around
to it!
△ ▽ • Reply • Share ›

Victoria > Webber • 3 years ago


I added you!
△ ▽ • Reply • Share ›

Daniel Griggs > Victoria


• 3 years ago
How's it going with github?
△ ▽ • Reply • Share ›

Victoria > Daniel Griggs


• 3 years ago
It's going well. I don't post much on
GitHub, but I am still learning
slowly!
△ ▽ • Reply • Share ›

Daniel Griggs > Victoria


• 3 years ago
That's great, don't be worried if you
learn slowly, I am too and it
sometimes it takes me ages to solve a
problem, like the ones on this site.
△ ▽ • Reply • Share ›

Tshi > Webber • 2 years ago


I used the program below:

# check if even or odd number

number = int(input("Enter a number: ").strip())

if ( b % d b % )
https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 9/14
9/22/21, 11:53 PM 02 Odd Or Even
if (number % 2 == 0 and number % 4 == 0):

output = "You entered {} which is an even number a


multiple of 4". format(number)

print (output)

elif number % 2 == 0:

output = "You entered {} which is an even number".


format(number)

print (output)

else:

output = "You entered {} which is an odd number".


format(number)

print (output)

num = int(input("Enter a number: ").strip())

check = int(input("Enter a number: ").strip())

if num % check == 0:

output = "The number {} devides evenly into {}".


format (check,num)

print (output)

else:

print("Numbers are evenly divisible.")


△ ▽ • Reply • Share ›

Anubhav > Webber • a year ago


bro, is it better to use formatted string or the way
you are doing it?

like, the

msg = "You gave me {} which is {} & divisible by


4!\n".format(inp,nType)

on line 6 would be:

msg = f"You gave me {inp} which is {nType} &


divisible by 4!\n"
△ ▽ • Reply • Share ›

Webber > Anubhav • a year ago


I prefer to use formatted string, but I don’t
think that was a thing in Python until after I
wrote my example
△ ▽ • Reply • Share ›

Anubhav > Webber • a year ago


oh alright.
△ ▽ • Reply • Share ›

Edward Hickcox • 3 years ago


https://gist.github.com/mig...
△ ▽ • Reply • Share ›

https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 10/14
9/22/21, 11:53 PM 02 Odd Or Even

Craig Steward • 3 years ago


https://gist.github.com/Cod...
△ ▽ • Reply • Share ›

Matt Coalson • 3 years ago


https://gist.github.com/mbc...
△ ▽ • Reply • Share ›

Sudhakar • 3 years ago


num=int(input("Enter any number: "))

if num%2==0:

print ("Entered number is Even")

else:

print ("Entered number is Odd")

********************************************************
num=int(input("Enter any number: "))

if num%4==0:

print ("Entered number is Multiplication of 4")

else:

print ("Entered number is not Multiplication of 4")

********************************************************
num1=int(input("Enter any number: "))

num2=int(input("Enter any number for devide: "))

if num1%num2==0:

print ("Conclusion: Evenly divided")

else:

print ("Conclusion: Evenly not divided")


△ ▽ • Reply • Share ›

Bo0m • 3 years ago


Question = int(input("enter an Odd or Even number -> "))

import time

if (.0 >= Question % 2):

print("even!")

else:

print("odd!")
△ ▽ • Reply • Share ›

Hypha • 3 years ago


https://gist.github.com/Hyp...
△ ▽ • Reply • Share ›

Compass • 3 years ago


https://gist.github.com/Bla...
△ ▽ • Reply • Share ›

Dante • 3 years ago


#!/usr/bin/env python2

n1 = int(raw_input('enter num1 '))

n2 = int(raw_input('enter num2 '))


https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 11/14
9/22/21, 11:53 PM 02 Odd Or Even
( _ p ( ))

if n1%4 == 0:

print n1, 'is a multiple of four'

elif n1%2 == 0:

print n1, 'is even'

else:

print n1, 'is odd'

if n1%n2 == 0:

print n1, 'is divisible by ',n2

else:

print n1, 'is not divisible by ',n2


△ ▽ • Reply • Share ›

wind_stopper • 3 years ago • edited


Solution (incl. Extra 1):

number = int(input('Enter a number: '))

print(("even" * (number%2 == 0)) + ", and multiple of 4" *


(number%4 ==0) or "odd")

Extra 2:

num, check = (input('enter two numbers: a, b: ').split(','))

print(('check divides evenly into num' * (


(int(num)%int(check)) == 0) or "doesn't divide evenly"))
△ ▽ • Reply • Share ›

Iltifath Ahmed • 3 years ago


https://gist.github.com/Ilt...
△ ▽ • Reply • Share ›

Андрей Помогаев • 3 years ago


def guessing():

numb = input('Type your number here> ')

if int(numb) % 2 == 0:

print('\nYour number is even!')

else:

print('\nYour number is odd!')

answer = input('\nDo you want to play again? (Y/N)> ')

if answer.lower() == 'y':

guessing()

else:

quit()

guessing()
1△ ▽ • Reply • Share ›

Blank trap > Андрей Помогаев • 3 years ago


Nice, I liked that you turned it into a game! lol
Here's mine.....

#Even or Odd exercise


https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 12/14
9/22/21, 11:53 PM 02 Odd Or Even

print('Type a number')

num = int(input('>>>'))

def div_by_4():

number = num % 4

if number == 0:

print('This number is divisble by four')


elif number > 0:

print('This number is not divisble by four')

def odd():

number = num % 2

if number > 0:

print('Odd')
see more

△ ▽ • Reply • Share ›

dosomethingwild > Андрей Помогаев


• 3 years ago
I like your idea so I made it a little more interacitve

######fuctions for game#######

def prt1():

number = int(input('Give me a random number '))

if number%2 == 0:

if number%4 ==0:

print('your number is an even number and a


multiple of 4')

else:

print('your number is an odd number')

elif number%2 > 0:

print('your number is an odd number')

else:

print('invalid input')

l i i (' ld lik l i
see more

△ ▽ • Reply • Share ›

Grey Hood > Андрей Помогаев • a year ago


I was doing the same thing but I did a loop instead
of a function... I'm not sure which one is easier
though.
△ ▽ • Reply • Share ›

Raphael • 3 years ago


https://gist.github.com/rtm...
△ ▽ • Reply • Share ›

Benjamin Joseph Kaufman • 3 years ago


num1 =input("Enter a number to find out if its odd or
https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 13/14
9/22/21, 11:53 PM 02 Odd Or Even
even")

num1=int(num1)

if num1%2 == 0:

print("This is even")

else:

print("This is odd")

Sponsored

Precious is So Skinny Now and Looks Gorgeous! (Photos)


Mortgage After Life

Bread Clips Can Be Used for So Many Things


YakudaChihinto

Put a sponge in the refrigerator - the reason will surprise you! What a great trick!
5 Min Tricks

These Are the Most Stunning Women of Hollywood


Gadgetheory

Photos of Helen Mirren In Her Heyday


TravelerMaster

Finding a Job in the USA from Lebanon Might be Easier Than You Think
Job USA | Search Ads

Starting a Business in Dubai Might be Easier Than You Think

Copyright Michele Pratusevich, 2014-2017.


Advertising disclosure

https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html 14/14

You might also like