Chapter 5

Practice for conditional statements

  • and

  • or

  • in

  • not

  • == etc

cubes = [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1200]

for cube in cubes:
  if cubes[3]!="37":
    print(f"not in the list")
not in the list
not in the list
not in the list
not in the list
not in the list
not in the list
not in the list
not in the list
not in the list
not in the list
not in the list

Not and in combined

if 37 not in cubes:
  print("37 is Not in the list")
37 is Not in the list

5-1. Conditional Tests:

Write a series of conditional tests. Print a statement describing each test and your prediction for the results of each test. Your code should look something like this:

print("Is this statement that 4th element of cubes list is 37 True? No it is", cubes[3]==37)
Is this statement that 4th element of cubes list is 37 True? No it is False
print("Is 37 in cubes?",37 not in cubes,"You know that it is", cubes[0]==True)
Is 37 in cubes? True You know that it is True

Create at least 10 tests. Have at least 5 tests evaluate to True and another 5 tests evaluate to False.

print(cubes[3]=="rt news")
False
print(37 in cubes)
False
cubes[0]==1
True
type(cubes) == list
True

5-2. More Conditional Tests:

You don’t have to limit the number of tests you create to 10. If you want to try more comparisons, write more tests and add them to conditional_tests.py. Have at least one True and one False result for each of the following:

Tests for equality and inequality with strings

'course' == 'learning'
False

Tests using the lower() method

if 'courses'.lower() == 'courses':
  print("We can make a string lower case without saving it as variable")
We can make a string lower case without saving it as variable

Numerical tests involving equality and inequality, greater than and less than, greater than or equal to, and less than or equal to

cubes[0]<=37
True
cubes[3] >= 34
True
'courses'>'learning'
False
cubes[3]==45
False

Tests using the and keyword and the or keyword

if 'courses'.upper() and 'courses'.lower() == 'Courses' :
  print("This will not print since one of condition is false")
if 'courses'.upper() or 'courses'.lower() == 'Courses' :
  print("This will print since one of all conditions is true")
This will print since one of all conditions is true

Test whether an item is in a list

if 37 in cubes:
  print("37 is in cubes list")
else:
  print("No 37 is not in cubes list")
No 37 is not in cubes list

Test whether an item is not in a list

37 in cubes
False

Practicing if and elif block without else

for cube in cubes:
  if cube == 1200:
    elements_of_cube = 37
  elif cube == 1:
    element_of_cube = 38
    
print(f"New element of cube we want to add is {element_of_cube}")
New element of cube we want to add is 38
for cube in cubes:
  if [38,512] not in cubes:
    cubes[1] = 37
  elif 38 in cubes:
    cubes[1] = 38
    
print(f"New cubes list is {cubes}")
New cubes list is [1, 37, 27, 64, 125, 216, 343, 512, 729, 1000, 1200]

5-3. Alien Colors # 1:

Imagine an alien was just shot down in a game. Create a variable called alien_color and assign it a value of ‘green’, ‘yellow’, or ‘red’.

Write an if statement to test whether the alien’s color is green. If it is, print a message that the player just earned 5 points.

alien_color = 'green'

if alien_color == "green":
  print("You have earned 5 points")
You have earned 5 points

Write one version of this program that passes the if test and another that fails. (The version that fails will have no output.)

# the one that fails if test
if alien_color == "red":
  print("You have not earned 5 points")
# one that passes if test
if alien_color == "green":
  print("You have not earned 5 points")
else: 
  print("You have earned 5 points")
You have not earned 5 points

5-4. Alien Colors # 2:

Choose a color for an alien as you did in Exercise 5-3, and write an if-else chain.

If the alien’s color is green, print a statement that the player just earned 5 points for shooting the alien.

if alien_color == "green":
  print("You have earned 5 points")
You have earned 5 points

If the alien’s color isn’t green, print a statement that the player just earned 10 points.

if alien_color != "green":
  print("You have lost 10 points")
else:
  print("You have earned 10 points")
You have earned 10 points

Write one version of this program that runs the if block and another that runs the else block.

if alien_color == "green":
  print("You have lost 10 points")
else:
  print("You have earned 10 points")
You have lost 10 points

5-5. Alien Colors # 3:

Turn your if-else chain from Exercise 5-4 into an if-elif- else chain.

If the alien is green, print a message that the player earned 5 points.

if alien_color == "green":
  print("You have earned 5 points")
else:
  print("You have earned 10 points")
You have earned 5 points

If the alien is yellow, print a message that the player earned 10 points.

if alien_color == "red":
  print("You have earned 5 points")
elif alien_color != 'yellow':
  print("You have earned 10 points")
You have earned 10 points

If the alien is red, print a message that the player earned 15 points.

if alien_color == "red":
  print("You have lost 15 points")
elif alien_color == 'yellow':
  print("You have lost 10 points")
else:
  print("You have earned 20 points")
You have earned 20 points

Write three versions of this program, making sure each message is printed for the appropriate color alien.

if alien_color == "red":
  print("You earned 20 points")
elif alien_color == "yellow":
  print("You earned 20 points")
elif alien_color =="red":
  print("You earned 15 points")
else:
  print("You earned 5 points")
You earned 5 points

5-6. Stages of Life:

Write an if-elif-else chain that determines a person’s stage of life. Set a value for the variable age, and then:

If the person is less than 2 years old, print a message that the person is a baby.

age = 23

if age <2:
  print("Person is a baby")

If the person is at least 2 years old but less than 4, print a message that the person is a toddler.

if age <2:
  print("Person is a baby")
elif age>2 and age<=4:
  print("Person is a todler")

If the person is at least 4 years old but less than 13, print a message that the person is a kid.

if age <2:
  print("Person is a baby")
elif age>2 and age<=4:
  print("Person is a todler")
elif age >=4 and age<13 :
  print("Person is a kid")

If the person is at least 13 years old but less than 20, print a message that the person is a teenager.

if age <2:
  print("Person is a baby")
elif age>2 and age<=4:
  print("Person is a todler")
elif age >=4 and age<13 :
  print("Person is a kid")
elif age >=13 and age<20 :
  print("Person is a teenager")

If the person is at least 20 years old but less than 65, print a message that the person is an adult.

if age <2:
  print("Person is a baby")
elif age>2 and age<=4:
  print("Person is a todler")
elif age >=4 and age<13 :
  print("Person is a kid")
elif age >=13 and age<20 :
  print("Person is a teenager")
elif age >=20 and age<65 :
  print("Person is a adult")
Person is a adult

If the person is age 65 or older, print a message that the person is an elder.

if age <2:
  print("Person is a baby")
elif age>2 and age<=4:
  print("Person is a todler")
elif age >=4 and age<13 :
  print("Person is a kid")
elif age >=13 and age<20 :
  print("Person is a teenager")
elif age >=20 and age<65 :
  print("Person is a adult")
elif age>65 :
  print("Person is an elder") 
Person is a adult

5-7. Favorite Fruit:

Make a list of your favorite fruits, and then write a series of independent if statements that check for certain fruits in your list.

Make a list of your three favorite fruits and call it favorite_fruits.

favorite_fruits = ['orange', 'apple','banana','dates','mango']

Write five if statements. Each should check whether a certain kind of fruit is in your list. If the fruit is in your list, the if block should print a statement, such as You really like bananas!

if 'orange' in favorite_fruits:
  print("You really like", 'oranges')
if 'banana' in favorite_fruits:
  print("You really like", 'bananas')
if 'dates' in favorite_fruits:
  print("You really like", 'dates')
if 'mango' in favorite_fruits:
  print("You really like", 'mangoes')
if 'pineapple' in favorite_fruits:
  print("You really like", 'pineapple')
You really like oranges
You really like bananas
You really like dates
You really like mangoes

5-8. Hello Admin:

Make a list of five or more usernames, including the name ‘admin’. Imagine you are writing code that will print a greeting to each user after they log in to a website. Loop through the list, and print a greeting to each user.

usernames = ['admin','shah','qaisar','amir','jabir']

for user in usernames:
  print(f"Welcome to the platform {user}")
Welcome to the platform admin
Welcome to the platform shah
Welcome to the platform qaisar
Welcome to the platform amir
Welcome to the platform jabir

If the username is ‘admin’, print a special greeting, such as Hello admin, would you like to see a status report? Otherwise, print a generic greeting, such as Hello Jaden, thank you for logging in again

for user in usernames:
  if user == "admin":
    print(f"Do you want to check system status?")
  else:
    print("Welcome to platform. Thankyou for logging again")
Do you want to check system status?
Welcome to platform. Thankyou for logging again
Welcome to platform. Thankyou for logging again
Welcome to platform. Thankyou for logging again
Welcome to platform. Thankyou for logging again

5-9. No Users:

Add an if test to hello_admin.py to make sure the list of users is not empty.

If the list is empty, print the message We need to find some users!

no_user = []

if no_user:
  for user in no_user:
    if user == "admin":
      print(f"Do you want to check system status?")
    elif user == 'shah':
      print("Welcome to platform. Thankyou for logging again")
else:
    print("we need to find more users")
we need to find more users

Remove all of the usernames from your list, and make sure the correct message is printed.

username = []

if username:
  for user in username:
    if user == "admin":
      print(f"Do you want to check system status?")
    elif user == 'shah':
      print("Welcome to platform. Thankyou for logging again")
else:
    print("we need to find more users")
we need to find more users

5-10. Checking Usernames:

Do the following to create a program that simulates how websites ensure that everyone has a unique username.

Make a list of five or more usernames called current_users.

current_users = ['admin','shah','qaisar','amir','jabir']

Make another list of five usernames called new_users. Make sure one or two of the new usernames are also in the current_users list.

new_users = ['fazal','rashid','Qaisar','Nabi','shehzad']

Loop through the new_users list to see if each new username has already been used. If it has, print a message that the person will need to enter a new username. If a username has not been used, print a message saying that the username is available.

for user in new_users:
  if user in current_users:
    print("username already taken")
  else:
    print("username available")
username available
username available
username available
username available
username available

Make sure your comparison is case insensitive. If ‘John’ has been used, ‘JOHN’ should not be accepted. (To do this, you’ll need to make a copy of current_users containing the lowercase versions of all existing users.)

for user in new_users:
  user = user.lower()
  if user in current_users:
    print("username already taken")
  else:
    print("username available")
username available
username available
username already taken
username available
username available

5-12

Already followed the code styling above.

End of Chapter 5