10/30/07.

Slides:



Advertisements
Similar presentations
2/7/2008. >>> Overview * boolean * while * random * tuples.
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Week 2 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Main task -write me a program
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
Week 4. >>> Overview * if else * returns * input.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
How to Create a Videogame By: Connor McCann. Java Java is one of many programming languages Java is used to run web browsers and most PC video games I.
If statements while loop for loop
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
1 CIS 2168 Data Structures and Algorithms in JAVA Brief Introduction to JAVA.
Built-in Data Structures in Python An Introduction.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Week 7 Review and file processing. >>> Methods Hello.java public class Hello { public static void main(String[] args){ hello(); } public static void hello(){
Chapter 5: Control Structures II
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Building java programs, chapter 5 Program logic and indefinite loops.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
1/10/2008. >>> About Us Paul Beck * Third quarter TA * Computer Engineering * Ryan Tucker * Second quarter TA * Computer.
10/9/07 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
8/2/07. >>> About Me Scott Shawcroft * Junior * Computer Engineering * Third Quarter TA * Creative Commons Intern * Small-time Open Source Developer
10/30/07. >>> Overview * boolean * randomness * while * tuples.
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
11/13/07. >>> Overview * lists/arrays * reading files * writing files.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
10/23/07. >>> Overview * if else * returns * input.
10/16/07.
Categories of loops definite loop: Executes a known number of times.
10/9/07 ______ < Moo? > \ ^__^ \ (oo)\_______ (__)\ )\/\
Lecture 5: Program Logic
Containers and Lists CIS 40 – Introduction to Programming in Python
Chapter 5: Control Structures II
Repetition-Counter control Loop
CSc 110, Spring 2017 Lecture 28: Sets and Dictionaries
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
Writing Functions( ) (Part 5)
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 6 More Conditionals and Loops
Building Java Programs
1/22/2008.
Building Java Programs
Topic 1: Problem Solving
while loops; logic; random numbers; tuples
Building Java Programs
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Building Java Programs
Using Objects (continued)
Building Java Programs
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Building Java Programs
Building Java Programs
Building Java Programs
Indefinite loop variations
Building Java Programs
Building Java Programs
Presentation transcript:

10/30/07

>>> Overview * boolean * randomness * while * tuples

>>> boolean Just like Java, there are boolean values. These values are True and False. >>> True True >>> False False >>> 2==3 >>> "this"=="this" >>> 2==3 and 4==4 >>> "milk" in ["milk","cookies"] >>> x = not 1 == 2 >>> x True False < > <= >= == != or and not in

>>> random Just like in Java, python also has random object. Here is an example: >>> import random >>> random.randint(0,9) 1 >>> random.choice(range(10)) 7 >>> x = [1,2,3,4] >>> x [1, 2, 3, 4] >>> random.shuffle(x) [2, 3, 4, 1] random.randint(a,b) returns an int between a and b inclusive random.choice(seq) returns a random element of the sequence random.shuffle(seq) shuffles a sequence in place (doesn't return it)

>>> while Loops in python are easy. The while loop translates easily from Java to python. sum = 0 input_number = 1 while input_number != 1: input_number = input("Number? ") sum += input_number print "The total was " + str(sum) sentinel.py 1 2 3 4 5 6 7 8 9 10 Scanner console = new Scanner(System.in); int sum = 0; int inputNumber = 1; while (inputNumber != -1) { System.out.print("Enter a number (-1 to quit): "); inputNumber = console.nextInt(); sum += inputNumber; } System.out.println("The total was " + sum); Sentinel.java 1 2 3 4 5 6 7 8 9 10

>>> dictionaries as points One of the biggest challenges with coding points is that two values need to be bundled together. The best type for this is the dict, short for dictionary, which is also known as a map or hash. While it is not a point it can hold our values with names. However, because it is not a point we cannot call .distance or .translate. In your port to Python you'll have to write functions of your own to do this. p = {"x":2,"y":3} # {<key>:<value>,...} p["x"]+=1 print p # {'y': 3, 'x': 3} def distance(p1,p2): # ??? pass dicts.py 1 2 3 4 5 6 7 8 9 10 A whaa? dictionary - an object which stores a value under a unique key

>>> while random boolean scott @ yossarian ~ $ python rps.py Welcome to Scott's rock-paper-scissors trainer. You can enter r, p or s for rock, paper and scissors appropriately. If you slip up and type anything else though you'll lose. How many games will it take you to get three in a row? Attack? r Loss: paper beats rock Attack? Attack? p Win: paper beats rock Attack? s Tie Win: scissors beats paper Win: rock beats scissors Congrats it took you 6 games.

>>> graphics review from drawingpanel import * p = DrawingPanel(300,300) g = p.getGraphics() # draw stuff here p.show() graphics.py 1 2 3 4 5 6 7 8 9 10

>>> circles from drawingpanel import * p = DrawingPanel(300,300) g = p.getGraphics() g.create_oval(0,0,100,100) g.create_oval(20,20,50,50,fill="pink") p.show() graphics.py 1 2 3 4 5 6 7 8 9 10

>>> rectangles from drawingpanel import * p = DrawingPanel(300,300) g = p.getGraphics() # x1,y1,x2 ,y2 g.create_rectangle(50,50,150,100) g.create_rectangle(20,20,21,21) p.show() graphics.py 1 2 3 4 5 6 7 8 9 10

>>> lines from drawingpanel import * p = DrawingPanel(300,300) g = p.getGraphics() # x1,y1,x2 ,y2 g.create_line(50,50,150,100) g.create_line(20,20,40,40,fill="red") p.show() graphics.py 1 2 3 4 5 6 7 8 9 10

>>> text from drawingpanel import * p = DrawingPanel(300,300) g = p.getGraphics() # x1,y1 (center) g.create_text(150,150,text="Hello,world",fill="green") p.show() graphics.py 1 2 3 4 5 6 7 8 9 10

© 2007 Scott Shawcroft, Some Rights Reserved Except where otherwise noted, this work is licensed under http://creativecommons.org/licenses/by-nc-sa/3.0 Python® and the Python logo are either a registered trademark or trademark of the Python Software Foundation. Java™ is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries.