PROGRAMMING
PROGRAMMING
UNIT – 3
PROGRAMMING
Learning objectives :
8P.01 Outline the purpose of program libraries.
8P.03 Identify and know how to use library functions in text-based
programs.
Learning objectives :
8P.04 Know how to develop text-based programs with conditional
(selection) statements.
Learning objectives :
8CT.06 Understand and use rules using AND, OR and NOT to create
logic within algorithms.
2. Ask the user a question and output whether their answer was correct.
Answer:
answer = int(input("What is 2 * 2?"))
if answer == 4:
print("Correct")
else:
print("Incorrect")
3. Ask the user to guess what number is stored in the computer. Output ‘Correct’ for a
correct guess or ‘Incorrect’ for an incorrect guess.
Answer:
number = 100
guess = int(input("Guess what number I am thinking of"))
if guess == 100:
print("Correct")
else:
print("Incorrect")
4. Ask the user to enter a number between 1 and 10 and output whether they did this
successfully or not.
Answer:
numberInput = int(input("Enter a number between 1 and 10"))
if numberInput >= 1 and numberInput <= 10:
print("Success")
else:
print("Incorrect")
5. Take two numbers as input from the user and output the number which is larger.
Answer:
first = int(input("Enter a number"))
second = int(input("Enter a number"))
if first > second:
print(first)
else:
print(second)
6. Ask the user a ‘yes’ or ‘no’ question and check if they answered yes with different
combinations of upper and lower case letters, such as ‘yes’, ‘Yes’ and ‘YES’.
Answer:
answer = input("Is a dolphin a mammal?")
if answer == "yes" or answer == "YES" or answer == "Yes":
print("Correct")
else:
print("Incorrect")
7. Ask the user to enter two test results. If either test result is greater than 90, tell them that
they passed.
Answer:
result1 = int(input("Enter result 1"))
result2 = int(input("Enter result 2"))
if result1 > 90 or result2 > 90:
print("Pass")
8. Ask the user to enter two test results. If both results are greater than 90, tell them that
they passed with distinction.
Answer:
result1 = int(input("Enter result 1"))
result2 = int(input("Enter result 2"))
if result1 > 90 and result2 > 90:
print("Distinction")
9. Ask the user to answer a question and use NOT to output whether they are incorrect.
Answer:
answer = int(input("What is 10 * 10?"))
if not(answer == 100):
print("Incorrect")
else:
print("Correct")
Learning objectives :
8P.02 Identify and describe data types in text-based programs,
including Integer, Real and Boolean.
What is a string?
Answer: one or more characters that can include letters, symbols and numbers that
are not used in mathematics calculations, such as telephone numbers or credit
card numbers.
What is the Boolean Data type?
Answer: Boolean Data type can only be one of two values either true or false. It means
Boolean data type will display ‘true’ with the binary number ‘1’, and ‘false’ with ‘0’.it is
used to indicate if something has occurred, or is valid or correct. If the flag is:
● true then it is positive, meaning that it has occurred, it is valid or it is correct
● false it is negative, meaning that it has not occurred, it is not valid or it is incorrect.
A set of programs that will require the use of data of different data types. For example:
1. Ask the user a series of questions that require answers as integers, reals and strings.
Store whether they answered each question correctly in a different variable as True for
correct, and False for incorrect.
Answer:
answer1 = int(input("What is 1 + 2?"))
if answer1 == 3:
answer1Result = True
else:
answer2Result = False
answer2 = float(input("What is 3 / 2?"))
if answer2 == 1.5:
answer2Result = True
else:
answer2Result = False
answer3 = input("Enter True if 2 = 3? Or False otherwise")
if answer3 == "True":
answer3Result = True
else:
answer3Result = False
2. Ask the user to enter True or False. Output a different message depending on their
answer.
Answer:
entered = input("Enter True or False")
if entered == "True":
result = True
else:
result = False
if result == True:
print("Yes you entered True")
else:
print("Oh no, you did not enter True")
3. Design a questionnaire and write a program to ask the user the questions and store their
answers.
Answer:
firstName = input("Enter your first name")age = int(input("Enter
your age"))
pocketMoney = float(input("Enter the amount of pocket money you
get"))
likeGames = input("Enter True if you like video games")
if likeGames == "True":
likeGames = True
else:
likeGames = False
4. Write a program that displays some data and asks the user for the most appropriate data
type for each. Output whether they are correct or incorrect.
Answer:
print("What data type is more appropriate for:")
answer1 = input("True or False")
if answer1 == "Boolean":
print("Correct")
else:
print("Wrong it is Boolean")
How could iterative development be useful when you have a very large program
to write?
Answer: The program can be split into smaller parts, with those parts being worked on
individually. New features and parts can then be added to the program throughout the
iterative process.
Learning objectives :
8P.10 Know how to test algorithms using suitable data.
8P.09 Explain the need for using a range of test data.
8P.08 Know how to develop and apply test plans