I Haz Codes

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Menu
Binary logic using IF

Syntax

if
a
<
0
and
a
>
10
:
 
print 'True block'
else
:
 
print 'False block'

About binary logic and IF

Binary logic uses AND, OR and NOT to link together conditions in if statements and loops. Understanding binary logic is crucial if you want to use if statements effectivly.


Below is the sample code from the video.

age = int(raw_input(&quot;Enter your age&quot;))
if age &gt;0 and age &lt;120:
	print &quot;age OK&quot;
else:
	print &quot;not ok&quot;

Key term

Binary logic – Using true and false statements to control a program.

Helpful links

Basics of binary logic 

Logic in python

PWNict logic in python and further logic 

Test your skill

1. To ensure a number is between 1 and 10 you can use the code below. What should replace the ???

num = int(raw_input(&quot;enter a number - &quot;))
if num &gt;=1 ???? num &lt;=10:
   print &quot;ok&quot;
Task 1: Answer SelectShow

2. Write a simple program, using just print, to display the logic tables for and, or and not.

Task 2: Answer SelectShow

3. Write a single if statement which will say “game on” if the user has more than 1 life left and has not won, otherwise it will say “stop game”. Use the variables below to get started –

gameWon = False
lives = 10
Task 3: Answer SelectShow

Leave a Reply

Your email address will not be published. Required fields are marked *

Information