Programming Holiday Questions
Programming Holiday Questions
while(this is true):
Ø There can be multiple ‘if’s (i.e. multiple conditions to
check):
print(“end”)
while(age<10):
while(age<10):
while-loop
print(age)
print(age)
if(age<16):
if(hifz==true)
Ø Simple while
do this
Ø While / else
print(“lunchtime at 12”)
age++
age++
Only prints ‘lunctime…’ if both conditions are true, and if first age=0
age=0
else:
is false then it doesn’t bother checking second
Ø Multiple ‘if’s can also be achieved with ‘and’ ‘or’
e.g.
9) Use the insert method to add "lemon" as the second item in the fruits list.
fruits = ["apple", "banana", "cherry"]
"lemon")
10) Use the remove method to remove "banana" from the fruits list.
fruits = ["apple", "banana", "cherry"]
print("No")
14) Print "1" if a is equal to b, print "2" if a is greater than b, otherwise print "3".
a = 50
b = 10
if(a==b):
print("1")
elif(a>b):
print("2")
else:
print("3")
15) Print "Hello" if a is equal to b, and c is equal to d.
if a == b and c == d:
print("Hello")
16) Print "Hello" if either a is equal to b, or c is equal to d.
if a == b or c == d:
print("Hello")
17) Print i as long as i is less than 6.
i=1
while(i < 6):
print(i)
i += 1
18) Stop the loop if i is 3.
i=1
while(i < 6):
if(i == 3):
i += 1
19) Loop through the items in the fruits list.
fruits = ["apple", "banana", "cherry"]
for x in fruits
print(x)
20) In the loop, when the item value is "banana", jump directly to the next item.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
21) Use the range function to loop through a code set 6 times.
for x in :
print(x)
dogs = []
if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel!")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
else:
print("Okay, this is a reasonable number of dogs.")
dogs = []
if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel!")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
elif len(dogs) >= 1:
print("Okay, this is a reasonable number of dogs.")
dogs = []
if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel!")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
elif len(dogs) >= 1:
print("Okay, this is a reasonable number of dogs.")
else:
print("I wish we had a dog here.")
23) Write code to check if number inputted by user is odd or even. Output appropriate messages to
reflect this.
24) Write a function that asks the user how old they are. Tell them if they are old enough to
vote. Then extend the program tell them how many years it is until they can retire (assume at age
65).
25) Write a function that asks the user to input a number between 1 and 20. Give a response which
indicates if the number is either within the range, too high or too low.
26) Complete the following task
Three is a Crowd
• Make a list of names that includes at least four people.
• Write an if test that prints a message about the room being crowded, if there are more than three
people in your list.
• Modify your list so that there are only two people in it. Use one of the methods for removing people
from the list, don't just redefine the list.
• Run your if test again. There should be no output this time, because there are less than three people in
the list.
• Add an else statement to your if tests. If the else statement is run, have it print a message that the room
is not very crowded.
Six is a Mob
• Save your program from Three is a Crowd - Part 2 under a new name.
• Add some names to your list, so that there are at least six people in the list.
• Modify your tests so that
Ø If there are more than 5 people, a message is printed about there being a mob in the room.
Ø If there are 3-5 people, a message is printed about the room being crowded.
Ø If there are 1 or 2 people, a message is printed about the room not being crowded.
Ø If there are no people in the room, a message is printed abou the room being empty.
30) Write a python program to print the square of all numbers from 0 to 10
31) Write a python program to find the sum of all even numbers from 0 to 10
32) Write a python program to read three numbers (a,b,c) and check how many numbers between ‘a’
and ‘b’ are divisible by ‘c’
33) Write code for the following task:
Growing Strength
• Make a variable called strength, and set its initial value to 5.
• Print a message reporting the player's strength.
• Set up a while loop that runs until the player's strength increases to a value such as 10.
• Inside the while loop, print a message that reports the player's current strength.
• Inside the while loop, write a statement that increases the player's strength.
• Outside the while loop, print a message reporting that the player has grown too strong, and that they
have moved up to a new level of the game.
for x in range(6):
print(x)
else:
print("Finally finished!")
for x in range(5):
print(x)
for x in range(10):
if x % 2 == 0:
continue
print(x)
my_list = range(0,10)
for i in my_list:
print(i)