Recap: If, elif, else If <True condition>:

Slides:



Advertisements
Similar presentations
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Advertisements

Introduction to Computing Science and Programming I
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Python quick start guide
An Introduction to Textual Programming
MOM! Phineas and Ferb are … Aims:
Guide to Programming with Python Chapter Three Branching, while Loops, and Program Planning: The Guess My Number Game.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of.
Copyright © Cengage Learning. All rights reserved. CHAPTER 9 COUNTING AND PROBABILITY.
Chapter 11 – Understanding Randomness 1. What is a random event? Nobody can guess the outcome before it happens. Let’s try an experiment. On the next page.
DiceRoller DiceRoller (object class) and DiceRollerViewer client class: Write a DiceRoller class (similar to Yahtzee) to: Allow the person to initially.
Understanding Randomness Chapter 11. Why Be Random? What is it about chance outcomes being random that makes random selection seem fair? Two things: –
For loops in programming Assumes you have seen assignment statements and print statements.
Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Make a dice challenge! This is a starter activity and should take 5 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.Copy the code below in.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Randomness Exploring Computer Science Lesson 4-10 – Part 2.
Objective of the lesson Use Blockly to make a dice for Snakes and Ladders All of you will: – Make an image which displays when you press a button Most.
REEM ALMOTIRI Information Technology Department Majmaah University.
Week of 12/12/16 Test Review.
Introduction to Python
Computer Programming Fundamentals
Guide to Programming with Python
Intro CS – Probability and Random Numbers
© 2016 Pearson Education, Ltd. All rights reserved.
ECS10 10/10
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.
If, else, elif.
Python - Iteration Iteration
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Random numbers Taken from notes by Dr. Neil Moore
CS190/295 Programming in Python for Life Sciences: Lecture 1
Writing Functions( ) (Part 5)
The relational operators
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
© Akhilesh Bajaj, All rights reserved.
Exception Handling.
Pick a number, any number …
CHAPTER 6 GENERAL-PURPOSE METHODS
Programming in JavaScript
And and or…and RANDOMNESS
Module 4 Loops.
COUNTING AND PROBABILITY
Writing Functions( ) (Part 4)
Topic 1: Problem Solving
Python programming exercise
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Programming in JavaScript
For loops Taken from notes by Dr. Neil Moore
Python Basics with Jupyter Notebook
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Exploring Computer Science Lesson 4-10 – Part 2
Chapter 13 Conditional Repetition
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Starter Look at the hand-out.
GCSE Computing.
Intro to Programming (in JavaScript)
Presentation transcript:

Recap: If, elif, else If <True condition>: Do this Elif <True condition>: Otherwise, do this Else:

Compound Statements and Random Numbers

Compound Statements if <condition 1> and <condition 2> You can use compound statements to make sure a block will only run if there is more than one condition to be satisfied if <condition 1> and <condition 2> Then do this if <condition 1> or <condition 2> Will only run if both conditions are true Will only run if at least one condition is true

When you’re finished… How could you make your program start over again and re-prompt the user if the user put in a negative number Add this to your existing code and email me .py file at mcraig@gvsd.org and turn in your grade sheet Read about random numbers and the random module: pages 50-53

Creating while loops While some condition is true, repeat something In coding: you might want to create a quiz show game, while there are questions left, keep playing the game. The while loops lets you do this

What’s a while loop Runs while a certain condition is true Examples: number = int(input("enter a number")) while number !=5 number = int(input(“Guess another number”))

Generating Random Numbers As much as users want consistent, predictable results from programs, sometimes what make the programs exciting is their unpredictability Random numbers can supply an element of surprise Python makes it easy to generate those random numbers Python generates random numbers based on a formula, so they are not truly random. This kind of random generation is called pseudorandom

Introducing the Craps Roller Program Open the Craps Roller Program from the Chapter 3 Folder Craps Roller replicated the dice roll of the fast-paced, casino game of craps. You don’t have to know anything about craps to appreciate the program. Craps Roller just simulates the roll of two, six-sided dice. It displayed the value of each and their total. To determine the dice values, the program uses functions that generates random number. Figure 3.2 shows the program

Importing the random Module The first line of code in the program introduces the import statement The statement allows you to import, or load, modules, in this case, the random module: import random

Modules Modules are files that contain code meant to be used in other programs. These modules usually group together a collection of programming related to one area. The random module contains functions related to generating random numbers and producing random results. When you import a module, it was either installed as part of your Python installation, or was downloaded and installed later on, prior to your running the program that imports it.

Generating Random Numbers You can generate random numbers using: randint() randrange()

randint() Function Produces a random integer Notice the program doesn’t directly call randint() It calls the function: random.randint(1,6) This accesses the function randint() through its module, random

Dot notation You can call a function from an imported module by giving the module name, followed by a period, followed by the function call itself (similar to string methods). This method of action is called dot notation. Using dot notation random.randint() means the function randint() that belongs to the module random. Dot notation can be used to access different elements of imported modules

randint() function Requires two integer argument values and returns a random integer between those two values, which may include either of the argument values. By passing the values 1 and 6 to the function, you are guaranteed to get back either 1,2,3,4,5 or 6

randrange() function produces a random integer Use a single, positive integer argument The function returns a random integer from, and including, 0, up to, but not including, that number Example: random.randrange(6) produces either a 0,1,2,3,4, or 5

Where’s the 6 in the Craps Rollar? randrange(6) is picking a random number from a group of 6 numbers – and the list starts with 0 If you add 1 to the result to get the right value for a die2: die2=random.randrange(6)+1 As a result, die 2 gets either a 1,2,3,4,5, or 6.

Research: What if you want to pick a random string out of a list? What function would you use in python? Do some research and turn in your answer.