Now that we've covered some of the basics such as variables, user input (string and integer), basic data types and operators, as well as control flow and decisions using IF and ELSE statements, let's try and create a few simple programs that perform calculations.
Download Python 3 here:
1.First, run the code to see how it works! Look up the actual conversion rate of pounds to japanese yen, and change the 150 accordingly.
2.Write another line of code which also converts the pounds input by the user into dollars, with an appropriate message. e.g "And that amount in dollars is____"
def convert_currency(): #Look up the actual conversion rate of pounds to japanese yen - change the 150 accordingly #Write another line of code which also converts the pounds to dollars. pounds=int(input("Enter No. of Pounds:")) yen=pounds*150 print("Converted into yen, that would give you:",yen) convert_currency()
1.Run the code below and note how it works..
2. There is an error in the code below. Can you fix it? The code needs to convert hours into minutes.
def convert_hours(): #There is an error in the code below. Can you fix it? The code needs to convert hours into minutes hours=int(input("Enter no. of hours")) minutes=60*x print(minutes) convert_hours()
1. This program has two steps to it. It adds up the test scores for Test1, Test2 and Test3 and then divides the total by 3 to get an average test result.
2. Adapt and add to the code to find the average for FIVE TEST SCORES. you willl need to add two more variables and then adapt the calculation part of the program.
def find_average(): #Add two more tests scores for test4 and test5. #Adapt the code to find the average for the five test scores test1=30 test2=100 test3=99 #find average of the three test scores total=test1+test2+test3 average=total/3 print("The average mark is:",average) find_average()
1. This program is a facebook friend entry simulation. It asks the user to enter the number of friends they have, and then it prints that number to the screen.
2. Add the functionality to also add more friends after the remove function (e.g ask the user to add a given number of friends).
3. After removing x amount of friends (that the user will put in), print the final remaining number of friends again to reflect the updated amount.
def friends_list(): #Add the facility to also add more friends after the remove function. #After removing x amount of friends (that the user specifies), print the remaining no. of friends again friends=int(input("Enter No. of Friends")) print("Thank you, you have,",friends,"friends") remove=int(input("Enter No. of Friends you wish to remove:")) remaining=friends-remove print("You have this many friends remaining:",remaining) friends_list() friends_list()
1.This program asks the user for a width and a height, and then prints the dimensions of the shape to the screen. I know you can't see the shape visually, and sometimes this can be difficult (or seem boring) but remember you are programming the important parts - the logic behind the scenes! As you learn more you will learn about how to create a graphical user interface in python too!
2. Change the width and height input and play around with making different shapes. See if you can find the area of the shape, and print the area to the screen once you have calculated it. Note that the area of a shape in this case, would simply be: area = width x height.
3. Difficult: Can you adapt the program to create another shape, such as a circle. What inputs would you need to create a circle?
def create_shape(): import turtle width=int(input("Enter width:")) height=int(input("Enter height:")) print("You have created a shape of width:",width,"and height:",height) create_shape()
Use your knowledge and skills from the completed tasks. Use the trinket below to complete your challenge.
Create a program that finds out the speed of a 100m runner. The formula for speed is speed= distance/time Ask the user to enter a distance (100) Ask the user to enter the time done by the runner. (e.g: 10) Print: The speed of the runner is: >> State any assumptions you make about time entry e.g. what is used -seconds/minutes