Decision Making - Logic and If Statements
Decision Making - Logic and If Statements
print('~' * 0)
print('~' * 1)
print('~' * 2)
print('~' * 3)
print('~' * 4)
print('~' * 5)
print('~' * 6)
print('~' * 7)
print('~' * 8)
Starter solution
~
~~
~~~
~~~~
~~~~~
~~~~~~
~~~~~~~
~~~~~~~~
Python Session 3
Topics in this session:
1. Comparison Operators
2. Logical Operators
3. If Statements
Comparisons and Logical Operators
Bolean: A data-type that is either True or False
Name Python
Equal to ==
Not equal !=
Greater than >
Less than <
Greater than or equal >=
Less than or equal <=
float() can convert strings to oats
Hint: remember to convert the input from a string to a decimal with float()
Solution
option The output should say whether the cost is within budget AND has a vegetarian option
if password == 'jumanji':
print('Success!')
password: jumanji
Success!
An if statement has the following:
1. The if keyword
2. A condition (comparison)
3. A colon
4. Body (indented four spaces)
condition
== 12
body
This program checks whether you are an admin and you have entered the right password:
if not is_good_choice:
print('Probably not a good idea')
if password == 'jumanji':
print('Success!')
else:
print('Failure!')
password: cluedo
Failure!
Here's the admin program rewritten to use else :
else:
print('Go away')
If your total meal costs more than £20 and you have a discount, the price will be reduced by 10%.
The program should print "Discount applied" or "No discount" depending on whether the discount
criteria was met.
if discount_applicable:
meal_price = meal_price * 0.9
print('Discount applied')
else:
print('No discount')
print('Total cost: {}'.format(meal_price))
else:
print('That is an average dog')
else:
print('That is an average dog')
import random
print(random_integer)
50
The randint() function generates a random number between two values
This program uses randint() to simulate dice with any number of sides
import random
If the random coin ip matches the choice input by the user then they win
Ohterwise if the random coin ip does not match the choice input by the user then they lose
import random
def flip_coin():
random_number = random.randint(1, 2)
if random_number == 1:
side = 'heads'
else:
side = 'tails'
return side
import random
def random_choice():
choice_number = random.randint(1, 3)
if choice_number == 1:
choice = 'rock'
elif choice_number == 2:
choice = 'scissors'
else:
choice = 'paper'
return choice
Ask the user to enter the following three things using input() :
If the colour matches, the users keeps the amount that was bet
If the number matches, the users wins double the amount that was bet
If the colour and number matches, the users wins 100 times the amount that was bet
When neither the colour or number matches the user wins 0
Output the amount the user won
def colour():
random_number = random.randint(1, 2)
if random_number == 1:
colour = 'red'
else:
colour = 'black'
return colour
1. Comparison operators
2. Logical Operators
3. If Statements
Question 1: Equals to (==) is a comparison operator. Name two more comparison operators
Question 2: What is the output of this code?
apples = 100