Page 236 - Computer Science V2.0 Class 11
P. 236
# Number of questions to generate
numQuestions = 10
# Loop through and generate questions
for i in range(numQuestions):
# Randomly select between word question and addition question
type = random.choice(["word", "addition"])
if type == "word":
question = generateWordQuestion()
else:
question = generateAdditionQuestion()
# Display the question
ans = input(question + ": ")
# Provide feedback
if type == "word" and ans.lower() == question.split()[-1].lower():
print("Correct! Great job!")
elif type == "addition" and ans.isdigit() and int(ans) == (num1+num2):
print("Correct! Well done!")
else:
print("Oops! That's not quite right. Let's try another one.")
# End of the game
print("Great effort! Play and Learn session is complete.")
playAndLearnGame()
4. Take a look at the series below:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55…
To form the pattern, start by writing 1 and 1. Add them together to get 2. Add the last two numbers: 1+2 = 3. Continue adding the
previous two numbers to find the next number in the series. These numbers make up the famed Fibonacci sequence: previous two
numbers are added to get the immediate new number.
Ans.
def fibonacci(num):
'''
Objective : To calculate the fibonacci series
Input Parameter : num - numeric value
Return Value : None
'''
x = 0
y = 1
if num == 1:
print(x)
else:
print(x,y, end=' ')
for i in range(2,num):
z = x + y
x = y
y = z
print(z, end=' ')
num = int(input("Enter a number: "))
222 Touchpad Computer Science (Ver. 2.0)-XI

