Python programming exercise

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Introduction to Computing Science and Programming I
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
An Introduction to Textual Programming
Introduction to Python
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
General Programming Introduction to Computing Science and Programming I.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Files Tutor: You will need ….
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Introduction to Computing Science and Programming I
Whatcha doin'? Aims: To start using Python. To understand loops.
Introduction to Programming
Repetition Structures
Python: Experiencing IDLE, writing simple programs
Python Loops and Iteration
Loops Upsorn Praphamontripong CS 1110 Introduction to Programming
Introduction to Python
Loops BIS1523 – Lecture 10.
Lesson 4 - Challenges.
Topics Introduction to Repetition Structures
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Introduction to Programming
CS 115 Lecture 8 Structured Programming; for loops
Useful String Methods Cont…
Topics Introduction to Repetition Structures
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Intro to PHP & Variables
Engineering Innovation Center
Iterations Programming Condition Controlled Loops (WHILE Loop)
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Ruth Anderson UW CSE 160 Winter 2017
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
More Loops.
More Loops.
Count Controlled Loops (Nested)
Topic 1: Problem Solving
Fundamentals of Data Structures
Topic 1: Problem Solving
We’re moving on to more recap from other programming languages
Coding Concepts (Basics)
Repetition Structures
Topics Introduction to Repetition Structures
Introduction to Programming
3.1 Iteration Loops For … To … Next 18/01/2019.
Computing Fundamentals
CSC115 Introduction to Computer Programming
CISC101 Reminders All assignments are now posted.
Flowcharts and Pseudo Code
CHAPTER 6: Control Flow Tools (for and while loops)
Topics Introduction to Repetition Structures
Unit 3: Variables in Java
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
Introduction to Programming
Software Development Techniques
Starter Look at the hand-out.
Presentation transcript:

Python programming exercise Creating a calculator

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

The code….

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'.

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

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

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

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

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 10 -1 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!")

Blast off code

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).

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

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

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

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

The code

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

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