Class 8 setCoords Oval move Line cloning vs copying getMouse, getKey

Slides:



Advertisements
Similar presentations
STRINGS IN PYTHON. Where have we seen strings before? #string type variables s = “hello” #obtaining keyboard input from the user choice = input(“Please.
Advertisements

Adapted from John Zelle’s Book Slides
Python Programming: An Introduction to Computer Science
CSC 110 Writing simple programs [Reading: chapter 2] CSC 110 C 1.
Vahé Karamian Python Programming CS-110 CHAPTER 2 Writing Simple Programs.
Chapter 2 Writing Simple Programs
Chapter 4 Objects and Graphics
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Python: Graphics
CSC 110 Objects and Graphics [Reading: chapter 4] CSC 110 E 1.
CSC 110 Sequences: Strings, Lists, and Files [Reading: chapter 5] CSC 110 F 1.
Chapter 5 Graphics.  We’ve been doing command line programming, but now it’s time to try GUI programming—programming in a graphical user interface, using.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Working with Strings Lecture 2 Hartmut Kaiser
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
Strings CS303E: Elements of Computers and Programming.
Python Programming, 1/e1 Programming Thinking and Method (5a) Zhao Hai 赵海 Department of Computer Science and Engineering Shanghai Jiao Tong University.
More Strings CS303E: Elements of Computers and Programming.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
Xiaojuan Cai Computational Thinking 1 Lecture 5 Objects and Graphics Xiaojuan Cai (蔡小娟) Fall, 2015.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
INPUT & VARIABLES.
Input and Output CMSC 120: Visualizing Information Lecture 4/10.
Interaction with Graphics Also displaying GIFs. Text Output on the graphics window There is a class called Text which will put the message on the graphics.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics.
CS 115 Lecture 7 Graphics – coordinate systems, Text, Entry, aliases Taken from notes by Dr. Neil Moore.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Vahé Karamian Python Programming CS-110 CHAPTER 4 Objects and Graphics.
CS 115 Lecture 6 Graphics Taken from notes by Dr. Neil Moore.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics Killer cars.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
CMSC201 Computer Science I for Majors Lecture 08 – Lists
Chapter 2 Writing Simple Programs
Graphics Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CS 106A, Lecture 4 Introduction to Java
Python Programming: An Introduction to Computer Science
Python: Experiencing IDLE, writing simple programs
Input and Output Upsorn Praphamontripong CS 1110
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
Computing Fundamentals
Python Programming: An Introduction to Computer Science
Variables, Expressions, and IO
CMSC201 Computer Science I for Majors Lecture 09 – Strings
Graphics Part I Taken from notes by Dr. Neil Moore
Graphics Part I Taken from notes by Dr. Neil Moore
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
Introduction to Strings
Introduction to Strings
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
CMSC201 Computer Science I for Majors Lecture 07 – Strings and Lists
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Data types Numeric types Sequence types float int bool list str
4. sequence data type Rocky K. C. Chang 16 September 2018
T. Jumana Abu Shmais – AOU - Riyadh
Graphics Part I Taken from notes by Dr. Neil Moore
Coding Concepts (Data- Types)
Basic String Operations
CS190/295 Programming in Python for Life Sciences: Lecture 3
Introduction to Strings
過故人莊 ~孟浩然 故人具雞黍,邀我至田家。 綠樹村邊合,青山郭外斜。 開軒面場圃,把酒話桑麻。 待到重陽日,還來就菊花。
15-110: Principles of Computing
CMSC201 Computer Science I for Majors Lecture 07 – Strings and Lists
Introduction to Computer Science
Introduction to Strings
Chopin’s Nocturne.
Presentation transcript:

Class 8 setCoords Oval move Line cloning vs copying getMouse, getKey Using getMouse to wait for user's data Entry box, getText, setText type() string indexing, slicing string repetition * concatenation + len – string length for <char> in <string>:

click a key and make the letter appear twice p=win.getMouse() k=win.getKey() klabel=Text(p,k+k) klabel.draw(win)

setCoords(lowerLeftX, lowerLeftY, upperRightX, upperRightY) What numbers to put in Point to get win=GraphWin("window",500,500) win.setCoords(-2,-2,3,3) Circle(Point( , ),2).draw(win)

Bounding box for an Oval Does the oval go through the point (0,0) Bounding box for an Oval Does the oval go through the point (0,0)? What will this oval look like? win=GraphWin("window",500,500) win.setCoords(-1,-1,4,4) ov=Oval(Point(0,0),Point(1,2)) ov.draw(win)

copying an object (and move) ovCopy=ov ovCopy.move(.5,1) where will ovCopy be? where will ov be?

copying an object ovCopy=ov ovCopy.move(.5,1) ov and ovCopy are the same Oval! there is only one Oval on the screen, not 2

cloning an object Where will ovClone be? Where will ov be? ovClone=ov.clone() ovClone.move(.5,-1) ovClone.draw(win)

cloning an object ovClone=ov.clone() ovClone.move(.5,-1) ovClone.draw(win) ov and ovClone are different Ovals ovClone has not yet been drawn, so we need

shape.move(dx,dy) mutator – changes some of the instance values of the shape if shape has already been drawn, draws shape in new location and undraws from old location

What is the output? win=GraphWin("window",500,500) win.setCoords(-1,-1,4,4) diag=Line(Point(0,0),Point(1,2)) diag.setFill("green") diag.setWidth(4) diag.draw(win)

Handling Textual Input # clickntype.py from graphics import * def main(): win = GraphWin("Click and Type", 400, 400) for i in range(10): pt = win.getMouse() key = win.getKey() label = Text(pt, key) label.draw(win) Python Programming, 3/e Python Programming, 3/e

Handling Textual Input There’s also an Entry object that can get keyboard input. The Entry object draws a box on the screen that can contain text. It understands setText and getText, with one difference that the input can be edited. Python Programming, 3/e Python Programming, 3/e

Handling Textual Input Python Programming, 3/e Python Programming, 3/e

Handling Textual Input # convert_gui.pyw # Program to convert Celsius to Fahrenheit using a simple # graphical interface. from graphics import * def main(): win = GraphWin("Celsius Converter", 300, 200) win.setCoords(0.0, 0.0, 3.0, 4.0) # Draw the interface Text(Point(1,3), " Celsius Temperature:").draw(win) Text(Point(1,1), "Fahrenheit Temperature:").draw(win) inputBox = Entry(Point(2,3), 5) inputBox.setText("0.0") inputBox.draw(win) output = Text(Point(2,1),"") output.draw(win) button = Text(Point(1.5,2.0),"Convert It") button.draw(win) Rectangle(Point(1,1.5), Point(2,2.5)).draw(win) Python Programming, 3/e Python Programming, 3/e

Handling Textual Input # wait for a mouse click win.getMouse() # convert input celsius = eval(inputBox.getText()) fahrenheit = 9.0/5.0 * celsius + 32 # display output and change button output.setText(fahrenheit) button.setText("Quit") # wait for click and then quit win.close() main() Python Programming, 3/e Python Programming, 3/e

Handling Textual Input Python Programming, 3/e Python Programming, 3/e

string and list processing

The String Data Type The most common use of personal computers is word processing. Text is represented in programs by the string data type. A string is a sequence of characters enclosed within quotation marks (") or apostrophes ('). Python Programming, 3/e

The String Data Type >>> str1="Hello" >>> str2='spam' >>> print(str1, str2) Hello spam >>> type(str1) <class 'str'> >>> type(str2) Python Programming, 3/e

The String Data Type Getting a string as input >>> firstName = input("Please enter your name: ") Please enter your name: John >>> print("Hello", firstName) Hello John Notice that the input is not evaluated. We want to store the typed characters, not to evaluate them as a Python expression. Python Programming, 3/e

The String Data Type We can access the individual characters in a string through indexing. The positions in a string are numbered from the left, starting with 0. The general form is <string>[<expr>], where the value of expr determines which character is selected from the string. Python Programming, 3/e

The String Data Type H e l o B b 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 >>> greet = "Hello Bob" >>> greet[0] 'H' >>> print(greet[0], greet[2], greet[4]) H l o >>> x = 8 >>> print(greet[x - 2]) B Python Programming, 3/e

The String Data Type H e l o B b 0 1 2 3 4 5 6 7 8 In a string of n characters, the last character is at position n-1 since we start counting with 0. We can index from the right side using negative indexes. >>> greet[-1] 'b' >>> greet[-3] 'B' Python Programming, 3/e

The String Data Type Indexing returns a string containing a single character from a larger string. We can also access a contiguous sequence of characters, called a substring, through a process called slicing. Python Programming, 3/e

The String Data Type Slicing: <string>[<start>:<end>] start and end should both be ints The slice contains the substring beginning at position start and runs up to but doesn’t include the position end. Python Programming, 3/e

The String Data Type H e l o B b 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 >>> greet[0:3] 'Hel' >>> greet[5:9] ' Bob' >>> greet[:5] 'Hello' >>> greet[5:] >>> greet[:] 'Hello Bob' Python Programming, 3/e

The String Data Type If either expression is missing, then the start or the end of the string are used. Can we put two strings together into a longer string? Concatenation “glues” two strings together (+) Repetition builds up a string by multiple concatenations of a string with itself (*) Python Programming, 3/e

The String Data Type The function len will return the length of a string. >>> "spam" + "eggs" 'spameggs' >>> "Spam" + "And" + "Eggs" 'SpamAndEggs' >>> 3 * "spam" 'spamspamspam' >>> "spam" * 5 'spamspamspamspamspam' >>> (3 * "spam") + ("eggs" * 5) 'spamspamspameggseggseggseggseggs' Python Programming, 3/e

looping through a string >>> len("spam") 4 >>> for ch in "Spam!": print (ch, end=" ") S p a m ! Python Programming, 3/e

The String Data Type Operator Meaning + Concatenation * Repetition Indexing <string>[:] Slicing len(<string>) Length for <var> in <string> Iteration through characters Python Programming, 3/e

Simple String Processing Usernames on a computer system First initial, first seven characters of last name # get user’s first and last names first = input("Please enter your first name (all lowercase): ") last = input("Please enter your last name (all lowercase): ") # concatenate first initial with 7 chars of last name uname = first[0] + last[:7] Python Programming, 3/e

Simple String Processing >>> Please enter your first name (all lowercase): john Please enter your last name (all lowercase): doe uname = jdoe Please enter your first name (all lowercase): donna Please enter your last name (all lowercase): rostenkowski uname = drostenk Python Programming, 3/e