Programs to build 2
Guess the Number
Write a simple guess the number game, where you decided what the number was, saved it in the program and then the user had to guess it (The original version of the game meant that the game terminated after one guess)
Extension: The game keeps going until the user has guessed the number correctly
Extension 2: The user only gets 5 guesses
You will need: if statement, print, input, int, variables
Interest Calculator
Write a program which works out how much interest you have earnt over a certain number of years.
You have £300, the interest rate is 5% and you are keeping it in the account for 5 years.
The program should tell you how much interest you earn each year, and a total.
Extension: The user can input their own amount, interest rate and number of years
You will need: for loop, print, variables (Extension: input, float)
Guess the Number (v2)
Amend your Guess the Number game so that it runs until the person has guessed the answer correctly. The number they are trying to guess should be chosen at random by the computer.
Extension: The user only gets 5 guesses
You will need: while, break, random
Logging in
Write a program which allows a user to "log on" to your system. Your program should ask the user for their user name and password, and check them against a username and password already stored in the program.
If they type both in correctly, they should get a message which says "Welcome", if they type either one in wrong they should get a message which tells them to try again. They should then be given the option of trying again.
You will need: if statement, boolean expression, variables, input, print
Showing posts with label for loop. Show all posts
Showing posts with label for loop. Show all posts
Monday, 15 October 2012
If, For, While, Random, Break, Boolean Expressions
If Statements
An if statement will run the code if a criteria is met.
See here for an example.
When you are checking the values, you can use the following operators:
== - equals to
!= - not equals to
> - greater than
< - less than
>= - greater than or equal to
<= - less than or equal to
For Loops
A for loop will repeat the code inside of it a certain amount of times.
You can define how often the code is run by changing the numbers in the brackets.
for x in range (0, 10):
print("Hello world")
In the example above x goes up by 1 each time until the whole code has been repeated 9 times.
While...
A while statement is used because you want something to run if a condition is met, or until a condition is no longer met.
num = int(input("Please enter a number: "))
while num != 5:
print("The number is not 5")
num = int(input("Please try another number :))
In the example above the user inputs a number. While the number they enter is not 5, the program outputs a message and then asks them to try again. The value in num is then changed to what the user types in the second time and the while statement is run again.
If the program did not ask for the user to input the number again after the output message, the while statement would just output the message "The number is not 5" forever....
You can use the same operators as you can for if in a while statement.
Import Random
Sometimes you want the computer to choose a number at random for you.
You do this by importing the random number generator (a lot like when we imported the turtle this needs to go right at the top).
import random
myNum = random.randint(0, 5)
The code above will randomly pick a number between the values in the brackets, and save whatever number it picks in the variable myNum.
Break
This is how you get a program to stop running.
break
Any code after this will not be run in your program - so be careful where you use it!
Boolean Expressions
Boolean expressions are how the computer checks if a value is true or false. You can also use boolean expressions to check if two or more values are the same or not.
With an if statement we checked one value with symbols, with boolean expressions we check two or more values with 'and' and 'or'.
The example below uses logging onto the school network as an example, although the true and false options stop the same, regardless of the circumstances.
EXAMPLE: Username and password - in order to log onto the school network both your user name and password need to be correct.
Username AND password.
If you type both of them in correctly then the computer sees the answer as being True and so it lets you log on.
If username is correct but password is incorrect, it sees the answer as being False and wont let you log on.
If username is incorrect but password is correct, it sees the answer as being False and wont let you log on.
If username is incorrect and password is incorrect, it sees the answer as being False and wont let you log on.
Username and password - lets pretend that you only need either your username or your password to be correct in order to log on. It doesn't matter if one is wrong so long as one is right.
Username OR password.
If you type both of them in correctly then the computer sees the answer as being True and so it lets you log on.
If username is correct but password is incorrect, it sees the answer as being True and so it lets you log on.
If username is incorrect but password is correct, it sees the answer as beingTrue and so it lets you log on.
If username is incorrect and password is incorrect, it sees the answer as being False and wont let you log on.
How to use boolean expressions in your code...
num1 = int(input("Please input a number: "))
num2 = int(input("Please input a second number: "))
if num1 == 4 and num2 == 6:
print("Well done you must be psychic!")
if num1 == 4 or num2 == 6:
print("Half way there!")
The code above asks the user to input two numbers. These are then stored in the variables num1 and num2.
An if statement then checks what they have typed in against some criteria.
On like 3, the if statement uses the "and" boolean operator to check if both numbers are the same as the ones in the program. In order for the program to consider this statement 'True' (and therefore run the code underneath it) both of the number have got to be the same as the ones saved in the program - in that order.
On link 5, the if statement uses the "or" boolean operator to check if either of the numbers are the same as the ones stored in the program. If they are correct with either of them the program will print out the "Half way there" message.
An if statement will run the code if a criteria is met.
See here for an example.
When you are checking the values, you can use the following operators:
== - equals to
!= - not equals to
> - greater than
< - less than
>= - greater than or equal to
<= - less than or equal to
For Loops
A for loop will repeat the code inside of it a certain amount of times.
You can define how often the code is run by changing the numbers in the brackets.
for x in range (0, 10):
print("Hello world")
In the example above x goes up by 1 each time until the whole code has been repeated 9 times.
While...
A while statement is used because you want something to run if a condition is met, or until a condition is no longer met.
num = int(input("Please enter a number: "))
while num != 5:
print("The number is not 5")
num = int(input("Please try another number :))
In the example above the user inputs a number. While the number they enter is not 5, the program outputs a message and then asks them to try again. The value in num is then changed to what the user types in the second time and the while statement is run again.
If the program did not ask for the user to input the number again after the output message, the while statement would just output the message "The number is not 5" forever....
You can use the same operators as you can for if in a while statement.
Import Random
Sometimes you want the computer to choose a number at random for you.
You do this by importing the random number generator (a lot like when we imported the turtle this needs to go right at the top).
import random
myNum = random.randint(0, 5)
The code above will randomly pick a number between the values in the brackets, and save whatever number it picks in the variable myNum.
Break
This is how you get a program to stop running.
break
Any code after this will not be run in your program - so be careful where you use it!
Boolean Expressions
Boolean expressions are how the computer checks if a value is true or false. You can also use boolean expressions to check if two or more values are the same or not.
With an if statement we checked one value with symbols, with boolean expressions we check two or more values with 'and' and 'or'.
The example below uses logging onto the school network as an example, although the true and false options stop the same, regardless of the circumstances.
EXAMPLE: Username and password - in order to log onto the school network both your user name and password need to be correct.
Username AND password.
If you type both of them in correctly then the computer sees the answer as being True and so it lets you log on.
If username is correct but password is incorrect, it sees the answer as being False and wont let you log on.
If username is incorrect but password is correct, it sees the answer as being False and wont let you log on.
If username is incorrect and password is incorrect, it sees the answer as being False and wont let you log on.
Username and password - lets pretend that you only need either your username or your password to be correct in order to log on. It doesn't matter if one is wrong so long as one is right.
Username OR password.
If you type both of them in correctly then the computer sees the answer as being True and so it lets you log on.
If username is correct but password is incorrect, it sees the answer as being True and so it lets you log on.
If username is incorrect but password is correct, it sees the answer as beingTrue and so it lets you log on.
If username is incorrect and password is incorrect, it sees the answer as being False and wont let you log on.
How to use boolean expressions in your code...
num1 = int(input("Please input a number: "))
num2 = int(input("Please input a second number: "))
if num1 == 4 and num2 == 6:
print("Well done you must be psychic!")
if num1 == 4 or num2 == 6:
print("Half way there!")
The code above asks the user to input two numbers. These are then stored in the variables num1 and num2.
An if statement then checks what they have typed in against some criteria.
On like 3, the if statement uses the "and" boolean operator to check if both numbers are the same as the ones in the program. In order for the program to consider this statement 'True' (and therefore run the code underneath it) both of the number have got to be the same as the ones saved in the program - in that order.
On link 5, the if statement uses the "or" boolean operator to check if either of the numbers are the same as the ones stored in the program. If they are correct with either of them the program will print out the "Half way there" message.
Subscribe to:
Posts (Atom)