Variables
Don't forget to convert inputs to numbers with int() (see below).
x = 100 y = int(input("Please enter the second number")) print(x / y)
Download the print.py file and save it to your own part of the server.
Once you have it opened you should see the following code:
x = 10 y = 20 z = 30 print(x * y / z)
-
1Your first task is to change the code to make better use of variables. For example, we could change the last lines as shown below:
result = x * y / z print(result)
Change the program so that the first number is taken in from the user -
2Write a new line in this program that multiplies 3.14 by a variable called r twice. E.g. (3.14 * r * r). For this you will want to take in a number from the user and assign it to a variable called r and then do the calculation separately.
r = int(input())
print(3.14 * r * r) -
3Write a program that takes in three numbers and stores them in variables before multiplying all three together and displaying it to the screen.
-
4Write a program that will store three numbers then get the total of them added together
-
5Write a program that asks the user for the names of three different items and then their prices before adding them all up, then displaying the names of the three items and the final cost. E.g. “Tomatoes, Potatoes and Oranges costs £3.50”
item1 = input()
item1_price = int(input()) -
6Write a program that asks for two numbers then displays the remainder of number one divided by number two.
-
7Write a program that takes three marks in and then displays the average of the three marks.
-
1Ask the user to enter a temperature in °C (Celsius).
Then convert it to °F (Fahrenheit) using the formula Fahrenheit = (Celsius × 9/5) + 32. -
2Ask the user for any two of: distance (d), speed (s). Then calculate the time it takes the user to run a that number of miles at that speed. Time = distance / speed.
-
3Ask the user for the length and breath of a triangle and calculate the area of it from that.
-
4Ask the user to enter the miles a car travelled and how many gallons of fuel it used. Using this calculate the miles per gallon it gets.
-
5Look at any existing programs you've made in the last topic (Input & Output) and modify them to use variables were appropriate.
Variable names
It is really important to name variables well. It's also important to know the different ways of writing variables. For example a % sign cannot be used within a variable name.
For each of the following state whether or not the variable name is suitable or not and why not.
-
1
pupil score -
2
width -
3
2ndPerson -
4
person5 -
5
forename&surname
Planning out a program
Whenever you develop a program, you should look at the inputs, processes and outputs to try and figure out how many variables you're going to need for your program.
-
1
A program is required to take in three test scores and add them together before getting the average of this number.
-
2
A program asks a user to enter 4 different payments they got. It then calculates how much they are left with after paying for their lunch for the month.