PYTHON PROGRAMMING
10
PART 2 – CONDITIONS (IF
STATEMENTS)
PROGRAMS
PROGRAM 1 – BOOLEAN OPERATORS (>,
>=, <, <=, ==, !=)
print(8 > 0) #True print(-8 > 2) #False print(5 < 4) #False print(0 == 0) #True print(7 == 3) #False print(4 != 3) #True print(5 >= 4) #True print(2 <= 1) #False |
PROGRAM 2 – ADDING A VARIABLE
a = 7 print(a >= 0) #True print(-8 > a) #False print(a == 7) #True print(a != 7) #False |
PROGRAM 3 – AND, OR and NOT (and, or,
not)
val = 7 print(val >= 0 and val <= 10) #True print(val > 100 or val == 7) #True print(not(val == 8)) #True # Note
# and gives True only if both sides are True
# or gives True if one or more sides are True
# not inverses the value |
TASK 1
What will
the following statements output to screen?
Remember that you can simply execute the code to check your answers.
a)
coco = 9
print(coco)
|
b)
yo = 9
print(yo == 5)
|
c)
a = 9
b = 7
print(a
> b)
|
d)
x
= 12
y
= 11
print(x
!= y)
|
e)
t
= 2
u
= 4
print(t
* t <= u)
|
f)
mark = 72
print("Pass?",
mark >= 50)
|
g)
g = 8
h = g ==
7
print(h)
|
h)
k = 12
print(k
>= 0 and k <= 100)
|
i)
woo =
True
print(not
woo)
|
j)
s1 = True
s2 =
False
s3 = s1
or s2
print(s3)
|
k)
a = True
b = 7
c = b != 9 and a
print(not
c)
|
|
PROGRAM 4 – BASIC IF STRUCTURE (if, indenting)
a = 2 if a > 5: print("Greater") print("Cool") print("Done") # Try unindenting the Cool line to see how # that changes the program. # Also try changing the value of a. |
PROGRAM 4B – USING IF
Write a
program that will ask the user for their D&D roll value. The user will likely enter a value
between 1 and 20.
Your program
simply checks if the roll is below 10.
If it is, it increases the value by 10. Then, the roll value if outputted back to
screen.
This would be
equivalent to some effect that stops the user from doing a bad roll.
dice = int(input("Enter
your roll"))
if
dice
< 10:
dice = dice + 10
print(dice)
|
TASK 2
Write a
program that asks the user for a number.
If the number is positive, it is simply outputted to screen
again. If the entered number is
negative, it is made positive and then outputted to screen.
SAMPLE IO
Enter a
number. 9
9
SAMPLE IO 2
Enter a number. -3
3
|
PROGRAM 5 – BASIC IF/ELSE STRUCTURE
(if, else)
m = 65 if m >= 50: print("Over fifty") else: print("Under fifty") # Try changing the value of m. |
PROGRAM 6 – MORE IF/ELSE
#Lego Batman's Security Software password = "ironman sucks" entered = input("What is the password?") if (entered == password): print("Entry has been granted!") else: print("Access denied")
|
TASK 3
Write the
program that will ask the user for their mark in this course and output
either You are passing. or You are failing. Make sure to test different marks to make
sure your program works well.
|
TASK 4
Write the
program that will ask the user for a number between 1 and 10
inclusively. If the number is in
fact in the range, your program should output Thank you for the number.
However, if the number is too low or too high, your program should
output Don’t you understand numbers?
|
PROGRAM 7 – IF/ELIF/ELSE (if, elif, else)
#Car dealership program print("Welcome to Pandemic Motors") money = float(input("How much money you got?")) if money < 20000: print("A used car it is.") elif money < 40000: print("A small car then.") elif money < 60000: print("A medium car or a small truck then.") elif money < 80000: print("Almost any vehicle can be yours.") else: print("You want a Tesla?") # Note that if statements are designed so # that you can never execute more than one # statement block. |
TASK 5
Write a
program that will ask the user for their IQ and respond with an appropriate
message of your choice. Here are the
IQ ranges that you should use:
121 and above – gifted (9 %)
111 to 120 – above average (16 %)
90 to 110 –
average (52 %)
89 and
below – below average (22 %)
|
|