Main Task Create a program that: Randomly generates a number between 1 -100 Asks the user to guess the number until they get it right Says whether the guess is too high or too low Says when they have guessed correctly, and quits Extension Task Modify the program so it records the number of guesses […]
Category: Uncategorized
Remember, indenting your code indicates it is inside a loop Always set your variable properly so the loop runs at least once Don’t forget the colon to start the conditional statement Tasks Type in the code and make sure that it works Change the code so it loops 20 times and prints out x each […]
One of the things that makes a computer useful is the ability to repeat an operation many times over at high speeds. This is called iteration or looping The FOR loop allows us to carry out a set of instructions a specific number of times The code within the loop must be indented The range […]
All the different ways of checking strings and characters on the last page return either TRUE (1) or FALSE (0). This means that they can be used with IF statements If you played around with the input string, you should have realised that islower() and isupper() only return true when the entire string is a […]
There are other useful functions for checking and manipulating characters and strings Reversing a string Sometimes you might need to reverse a string. This may be to make a working with binary strings easier, or to help create random passwords. Tasks Type in the code and make sure that it works Add code to reverse […]
Characters can be checked and compared in much the same way as numbers, using the same symbols Less than (<) and greater than (>) work if you think about the alphabet as going from low (a) to high (z) Remember that lowercase and uppercase letters are recognised as different characters by the computer Symbol Example […]
We can check strings using the same methods as with numbers A character is simply a string with a length of 1 The in operator is a quick way of checking if one string or character is contained within another – useful to check user input against an allowed list of characters or symbols Some […]
Main Task Create a basic calculator program that will take two numbers, and perform one of the following functions depending on what the user chooses: add subtract multiply divide You should assume that all user input will be valid whole numbers. You should ask the user which function they wish to perform. The output should […]
Logical operators allow you to combine checks on variables The two operators are OR and AND OR will be true if either condition is met AND will be true when both conditions are met In Python, they are always written in lowercase Tasks Type in the code and test that it works Modify the program […]
It is very important to be able to check the type of data entered If you try to convert data to a type that won’t ‘fit’, your program will crash The code below will crash if a non-numeric input is supplied The example below uses the Try/Except statement to catch the error The program attempts […]