Software Development (Python Examples) Flashcards

1
Q

Selection(elif)+ AND operator

A

age = int(input(‘Enter age ‘))
print(‘*************’)
if age < 5:
print(‘Attend Nursery’)
elif age > 4 and age < 12:
print(‘Attend Primary’)
elif age >= 12 and age < 19:
print(‘High School’)
else:
print(age, ‘is too old to attend school’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Fixed Loop

A

message = input(‘Enter the message to be repeated ‘)
print(‘*************’)
for counter in range (1,6):
print (message)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Conditional loop

A

guess = ‘’ “
attempts = 0
password = ‘secret’

while attempts != 3 and password != guess:
attempts = attempts + 1
guess = input(‘Guess my password ‘)
print(‘*************’)
if attempts == 3 and password != guess:
print(‘Bad luck no more guesses’)
else:
print(‘You guessed correctly on attempt‘, attempts)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Pre defined string functions

A

message = ‘Pre-defined functions are in-built into language’
print(message.upper())

school = ‘St Mungo\’S High School’
print(school.lower())

numChars = len(input(‘Type in a message to be counted’))
print(‘Number of characters counted is ‘, numChars)
print(len(‘Computer Science is my favourite subject’))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Random function

A

Import random
Dice = random.randInt(1,6)
Print (‘the roll of the dice is’,Dice)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Round Function

A

avgTime = 0.00
time1 = float(input(‘Enter time of first lap ’))
time2 = float(input(‘Enter time of second lap ’))
time3 = float(input(‘Enter time of third lap ’))
avgTime = (time1 + time2 + time3)/3
print(‘Average time, 3 decimal places is ’, round(avgTime,3)
print(‘Average time, 2 decimal places is ’, round(avgTime,2)
print(‘Average time, 1 decimal places is ’, round(avgTime,1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

1D Array of strings

A

pupils = [‘’] * 5
grades = [‘’] * 5

for index in range(0, 5):
pupils[index] = input(‘Enter name of pupil ’)
grades[index] = input(‘Enter pupil’s grade ’)
print (‘******‘)
for index in range(0, 5):
print (pupils[index],‘achieved grade‘, grades[index])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Array of numbers

A

numbers = [0] * 12
numbers = [78, 54, 96, 33, 72, 47, 51, 88, 39, 26, 17, 99]
count = 0
print (numbers)
for index in range(0, 12):
if numbers[index] > 50:
count = count + 1
print (‘There are ‘, count, ‘numbers in the list > 50‘)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Input validation

A

number = int(input(‘Enter a number ‘))
while number < 1 or number >100:
print (number, ‘is an invalid number’)
number = int(input(‘Re-enter number (1 to 100) ‘))
print (‘Valid number is ‘, number)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Running total within a loop

A

total = 0
for counter in range (0,5):
score = int(input(‘Enter score ‘))
total = total + score
print (‘The total is’, total)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly