2 – Selection (Level 4)

Sample Code

 Simple Selection

surname = input("Enter your surname\n")
gender = input("Are you M or F?\n")
if gender == "M":
    print("Hello Mr " + surname)
else:
    print("Hello Ms " + surname)

Operators

day = input("Name a day that starts with the letter S\n")
if day == "Saturday" or day == "Sunday":
    print("Correct")
else:
    print("Wrong")
year = int(input("Input your year\n"))
if year>=7 and year <=9:
    print("You are in KS3")
else:
    print("You are not in KS3")

Exercises

  1. Prompt the user as follows: “What’s the capital of France?” Output “Correct” if they enter “Paris”, output “Incorrect” otherwise
  2. Prompt the user as follows: “Name a month that starts with the letter A”: Output “Correct” if they enter “April” or “August”, output “Incorrect” otherwise
  3. Prompt the user as follows: “Name a Beatle”. Output “Correct” if they enter “John”, “Paul”, “George” or “Ringo”, output “Incorrect” otherwise
  4. Prompt the user to enter their age. If they are 18 or over output “You are old enough to vote.” Otherwise, output “You are too young to vote”
  5. Prompt the user to enter their age. If they are aged between 4 and 16 output “You should be at school”
  6. An online whisky shop charges for shipping as follows: One bottle, £5.99; two to five bottles, books £7; more than five bottles, free. Prompt the user to enter the number of bottles bought and output the shipping cost.
  7. An online bookshop charges shipping as follows: Orders less than £10, £2.99; orders £10 and over, free; add on £2.50 for all orders if next day delivery is selected. Prompt the user to enter the cost of the order, and then prompt for next day delivery. Output the shipping cost.

Extension

The modulo operator (%) is very useful. It works out the remainder when one number is divided by another. So
10 % 2 = 0 (Because 10/2 = 5 remainder 0)
10 % 3 = 1 (Because 10/3 = 3 remainder 1)
Use the modulo operator to solve the following problems.

  1. Prompt the user to enter a number. Output if the number is odd or even.
  2. Prompt the user to enter a humber. Output Fizz if the number is divisible by 3, otherwise just output the number
  3. Extend the above problem so that the computer will output Fizz if the number is divisible by 3, output Buzz if the number is divisible by 5 and otherwise just output the number.
  4. Now extend the above problem again so that the computer will output Fizz if the number is divisible by 3, output Buzz if the number is divisible by 5, output Fizz Buzz if the number is divisible by both 5 and 3 and otherwise just output the number.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.