Python Exercises
Python Exercises
Write a program that uses a for loop to output all of the even numbers from 0
to 100 (you could use the Mod operation here).
2. Write a program that uses a for loop to output all of the odd numbers from 0
to 100 (you could use the Mod operation here).
4. Create a program that will allow the user to enter a monthly payment
amount. The program will then show the accumulative payment for a year.
5. Create a program that will loop 10 times and output the contents of “counter”
during each loop.
6. Copy the following program outline, then check that it runs without errors:
pocketMoney = 0.01
for week in range(10):
print("\nIt is week ",week)
print("You will get £ ",pocketMoney)
input("\nPress ENTER to exit program")
7. Change the for() loop so that it counts the weeks from 1 to 26. Check that it
works as expected.
8. Add one line of code inside the loop to double the amount of pocket money.
Hint: pocketMoney = ……. Check that it works as expected.
9. Finally, add a line of code at the start to create a variable called totalMoney.
totalMoney = 0
Inside the loop, keep a running total of the total pocket money you will have
collected so far. Only print this value at the END of the program (after exiting
the loop).
1. Create a program that checks a pre-set password entered by the user.
2. Ask the user to tell the program facts about themselves (don’t need to store
these) until the user enters the word exit.
a. Can you convert the user’s input and while loop condition so that it doesn’t
matter what version of the word exit is used (exit, EXIT, Exit)
b. Amend the loop so that it will end if the user enters “Exit” or “End”
3. Repeatedly ask the user to enter a number between 1 and 10. While the user
doesn’t enter a number between these parameters the code will repeat. Like the
IF statement you will need to use the OR condition in your syntax.
4. You can use variables to keep count of things during your program. For
example, x = x + 1 repeated 10 times would increase the value of x by 10. This
is a very handy trick to master!
See what the following sets of code produces:
x=0
while x <= 5:
print(“Hello”)
x=x+1
counter = 5
while counter > 0: print(“12345”) counter = counter -1
5. Using this method, create a program that asks a user to enter a number. The
program will then output the squares of that number that are below 200:
a. Make sure you get your data type correct!
b. You actually need two variables for this. Can you spot why? Getting it wrong
is often the easiest way to finding the answer!