Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python programming exercise

Similar presentations


Presentation on theme: "Python programming exercise"— Presentation transcript:

1 Python programming exercise
Creating a calculator

2 Step one –in writing lets create some pseudo code for creating a calculator in python Statement to read multiplication tables Use our old friend n = int(input(“which multiplication tables would you like”:”)) n = int(input( *This means, wait for some input from the user and, treating it as a whole number, save it in a variable called 'n'. 3.We need to create a for loop “for” loop makes the variable 'i' take on the values from 2 to 14. NB:The sequence the loop iterates over is generated by the return value of the range()function i gets 0 then prints 0 then goes to next number 1 in range you have given it and prints 4. Then we need to say what ever number an end user has used multiply that to create the times table for that number entered –ie if end user enters 2 , then the times table will be 2 and the table created will multiply to 14 in total as that is what was specified in the (for I in range(2,14)The range that will be iterated over is 2-14

3 The code….

4 Let's turn it into a program.
First make a “python” folder in your documents. Use File > New Window to open the editor. Save your program as “tables1.py” I like to start each program with its name as a comment. A print() statement to introduce the program. This means, wait for some input from the user and, treating it as a whole number, save it in a variable called 'n'.

5 And now for a different type of loop
While loops We are going to use while loops in our program calculator so we need to know how to use them Basically, we use a “while” loop when we don't know how many times a loop will need to run. Otherwise, it's usually best to use a “for” loop

6 While loops Type the following in to python and run it
What does it do????????????????????????????

7 Using while loops with numbers
Type the following code in what does it do?

8 What happens when you change in this case the 4 to a 10 ?
Anything different from previous?

9 Use of while and else You can also use while in conjunction with else
In the code we are now going to practice we are going to import the time module We are going to set the count down to start from 10 We are going to use a while loop to say countdown to 0 from 10 and stop when you get there To countdown we are saying that C (our variable) is equal to C -1 If we don’t have that line of code the program will just print out number 10 continuously Time.sleep refers to the module and sets a delay in the numbers counting down from by one second Once the countdown has completed then the statement blast off will be printed import time c=10 while c > 0: print(c) c=c-1 time.sleep(1) else: print("BLAST OFF!")

10 Blast off code

11 Input validation We need to make sure that the input is correct to our program One way you can do this is through using input validation In our program, we want the user to enter a whole number. The program tries to turn what they typed into an integer. If this doesn't work, the program crashes. Which is bad. So we can use “try:” and “except:”. These work as you might expect, they try to do the thing we want and let us specify what should happen if it fails. That way the program will not crash (we hope).

12 Input Validation Enter this program and try it out.
It will be a useful reminder of how to use “try:”.

13 Creating a calculator pt 2
What we are going to do now is create a condition -that if the program user wants to quit they can press q at any time and the program will quit

14 While true creates a continuous loop that is only broken
Num is a variable We are saying that the user of the program is going to enter a number If the input that the user of the program enters is the letter q then the program will end using the break command

15 Next stage of code We do that by use of try and except
next we need to make sure that what is entered by end user as a number is validated We do that by use of try and except We are saying that what is going to be entered by the end user is a number we can tell that in the code by use of num =int(num) then we need a for I in range to iterate a range of number We are saying that the data the user enters will be a number we are then saying that the range of the multiplication table produced is 1-14 Now we are going to put in code that provides for a space between the numbers to format the result We even the output up by adding a spaceThe end = '' bit means “don't add a newline The following line of code deals with the formatting of the output

16 The code

17 While True creates an infinite loop
Ends program if condition of user input of a q is met Range of multiplication table Second condition that reformats numbers out put Use of end makes sure data is kept on same line and is spaced apart

18 Extend your code! Add in import time and add in other print strings

19


Download ppt "Python programming exercise"

Similar presentations


Ads by Google