Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

CLASS – CAM 7

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.

Introduction to Program Library:


What is a Program Library?
Answer: A pre-written and pre-tested program that can be called in another program.

How do you use a program library in Python?


Answer:You import the library and then call the functions.

What are some examples of program libraries in Python?


Answer: random and math.

Why are program libraries useful?


Answer: They save time in writing new functions. They are also pre-tested so should already
work.

Learning objectives :
8P.04 Know how to develop text-based programs with conditional
(selection) statements.

Introduction of Conditional or Selection statements:


What are the three component parts of a conditional statement?
Answer: the condition; the code to run if true; the code to run if not true

What is a conditional statement?


Answer: A question that has a true or false answer.

What is the symbol for a condition in a flowchart?


Answer: a diamond

What are the conditional operators that we can use?


Answer: <, >, =, !=, <=, >=

What is the format of conditional statements in Python?


Answer: IF:
if condition:
code to run if true
IF else:
if condition:
code to run if true
else:
code to run if false

Write any one example of conditional statements in Python?


Answer:
number = 10
if number == 10:
print("Yes")
else:
print("No")

Learning objectives :
8CT.06 Understand and use rules using AND, OR and NOT to create
logic within algorithms.

Introduction of AND, OR and NOT:

● AND requires both conditions to be true (1 1) to result in true.

● OR requires one or both conditions to be true (1 1, 1 0, 0 1).

● NOT reverses the statements (1 becomes 0, 0 becomes 1).

Programs related to Conditional Statements (IF, ELSEIF, ELSE) and


AND, OR, NOT:
Kindly note: All are requested to solve and run the below given programs in any compiler
like Python IDLE. Paste the screenshots of code and output in your notebooks.
1. Take a number as input from the user and check whether it is greater than 10. Repeat
by checking if it is less than 10.
Answer:
numberInput = int(input("Enter a number"))
if numberInput > 10:
print("Greater than 10")
else:
print("Less than or equal to 10")

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 data type in programming?


Answer: data that is put into categories, so the program knows what is expected

What data types do you already know?


Answer: Integer, Real and String

What is the difference between an Integer and a Real number?


Answer: an Integer is a whole number, a Real number is a decimal number

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.

Explain examples of different data and its data type.

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")

answer2 = input("1, 4, 6, 12")


if answer2 == "Integer":
print("Correct")
else:
print("Wrong they are integers")

answer3 = input("horse, spider, rabbit")


if answer3 == "String":
print("Correct")
else:
print("Wrong it is a string")

answer4 = input("1.22, -2.93, 99.00001")


if answer4 == "Real":
print("Correct")
else:
print("Wrong they are real numbers")
Learning objectives :
8P.07 Use an iterative process to develop programs

What do you mean by iteration or repetition?


Answer: it is about doing the same thing more than once.
Explain the term “Iterative process” in software development?
Answer: An iterative process is the process of writing part of a program, testing it and editing it,
then adding to the program, testing it and editing it, and then adding to it continually until a final,
working, program is produced.

What is iterative development?


Answer: Repeatedly editing and testing a program until it is complete.

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

Why do we need to test programs?


Answer: to make sure that they work; to check there are no errors; to check that they give
suitable responses to unexpected input

How do we test that a program works?


Answer: we use the program, for example by entering data and pressing buttons, then check
whether it does what is expected to meet its criteria.

Program- Display numbers between 1 and 10.(10 will be included)


number = int(input("Enter a number between 1 and 10 inclusive"))
if number >= 1 and number <= 10:
print("Success")
else:
print("Wrong")

● 5 will output Success

● 15 will output Wrong

● 10 will output Success

● A sample test table for the above program:


Why did you test your program?
Answer: To make sure it works and to check whether it does what is expected to meet
its criteria. Testing also checks whether the program is able to give a suitable response
to an unexpected input.

Why should you test a program using a range of data?


Answer: It may work with one input but not any other inputs.
Testing with a range of data also checks whether the program works in all reasonable
cases.

You might also like