cs 106a: variables reviewweb.stanford.edu/class/cs106a/handouts/variablesreview.pdf ·...

101
CS 106A: Variables Review Wednesday, May 13

Upload: others

Post on 12-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

CS 106A: Variables ReviewWednesday, May 13

Page 2: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Today...

Page 3: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

1. What is a Variable?2. Arithmetic On Variables3. Variables and Control Flow4. Diagnostic Problem 4, Redux

Today...

Page 4: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

What is a Variable?Suitcases and Luggage Tags

Page 5: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Variables are like baggage tags that attach to suitcases.

What is a Variable?

Page 6: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Variables are like baggage tags that attach to suitcases.

Doesn’t assign any baggage tags, but does use suitcases:print(106)

What is a Variable?

Page 7: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Variables are like baggage tags that attach to suitcases.

Doesn’t assign any baggage tags, but does use suitcases:print(106)

What is a Variable?

Page 8: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Variables are like baggage tags that attach to suitcases.

Doesn’t assign any baggage tags, but does use suitcases:print(106)

What is a Variable?

int106

Page 9: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

What is a Variable?

Variables are like baggage tags that attach to suitcases.

Doesn’t assign any baggage tags, but does use suitcases:print(106)

Creates a suitcase and assigns a baggage tag to it:class_num = 106print(class_num)

int106

Page 10: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

What is a Variable?

Variables are like baggage tags that attach to suitcases.

Doesn’t assign any baggage tags, but does use suitcases:print(106)

Creates a suitcase and assigns a baggage tag to it:class_num = 106print(class_num)

int106

Page 11: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

What is a Variable?

Variables are like baggage tags that attach to suitcases.

Doesn’t assign any baggage tags, but does use suitcases:print(106)

Creates a suitcase and assigns a baggage tag to it:class_num = 106print(class_num)

int106

int106

Page 12: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

What is a Variable?

Variables are like baggage tags that attach to suitcases.

Doesn’t assign any baggage tags, but does use suitcases:print(106)

Creates a suitcase and assigns a baggage tag to it:class_num = 106print(class_num)

int106

int106

class_num

Page 13: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Okay, How Do I Use Them?

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

Page 14: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Okay, How Do I Use Them?

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

name = input("What is your name? ")print("Hiya, " + name + "! I'm Python.")

Page 15: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Okay, How Do I Use Them?

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

name = input("What is your name? ")print("Hiya, " + name + "! I'm Python.")

We need to keep track of what the user enters to use

it in the greeting.

Page 16: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Okay, How Do I Use Them?

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

name = input("What is your name? ")print("Hiya, " + name + "! I'm Python.")

We need to keep track of what the user enters to use

it in the greeting.

Page 17: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Okay, How Do I Use Them?

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

name = input("What is your name? ")print("Hiya, " + name + "! I'm Python.")

This is the problem that variables solve: they allow you to keep track of important data throughout the execution of your program.

We need to keep track of what the user enters to use

it in the greeting.

Page 18: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Okay, How Do I Use Them?

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

name = input("What is your name? ")print("Hiya, " + name + "! I'm Python.")

This is the problem that variables solve: they allow you to keep track of important data throughout the execution of your program.

We need to keep track of what the user enters to use

it in the greeting.

str"Parth"

Page 19: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Okay, How Do I Use Them?

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

name = input("What is your name? ")print("Hiya, " + name + "! I'm Python.")

This is the problem that variables solve: they allow you to keep track of important data throughout the execution of your program.

We need to keep track of what the user enters to use

it in the greeting.

str"Parth"nam

e

Page 20: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

Another Use: Modifying Variables

Page 21: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

pixel = image.get_pixel(x, y)pixel.red = 255pixel.green = 0pixel.blue = 0

Another Use: Modifying Variables

Page 22: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

pixel = image.get_pixel(x, y)pixel.red = 255pixel.green = 0pixel.blue = 0

Another Use: Modifying Variables

We need to hold on to the pixel...

Page 23: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

pixel = image.get_pixel(x, y)pixel.red = 255pixel.green = 0pixel.blue = 0

Another Use: Modifying Variables

We need to hold on to the pixel...

...so that we can modify its attributes here.

Page 24: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

pixel = image.get_pixel(x, y)pixel.red = 255pixel.green = 0pixel.blue = 0

Another Use: Modifying Variables

We need to hold on to the pixel...

...so that we can modify its attributes here.

PixelR: 100

G: 200B: 30

Page 25: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

pixel = image.get_pixel(x, y)pixel.red = 255pixel.green = 0pixel.blue = 0

Another Use: Modifying Variables

We need to hold on to the pixel...

...so that we can modify its attributes here.

PixelR: 100

G: 200B: 30

pixel

Page 26: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

pixel = image.get_pixel(x, y)pixel.red = 255pixel.green = 0pixel.blue = 0

Another Use: Modifying Variables

We need to hold on to the pixel...

...so that we can modify its attributes here.

PixelR: 255

G: 200B: 30

pixel

Page 27: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

pixel = image.get_pixel(x, y)pixel.red = 255pixel.green = 0pixel.blue = 0

Another Use: Modifying Variables

We need to hold on to the pixel...

...so that we can modify its attributes here.

PixelR: 255

G: 0B: 30

pixel

Page 28: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Use a variable to keep track of important data in your program that you’ll need to refer to in the future.

pixel = image.get_pixel(x, y)pixel.red = 255pixel.green = 0pixel.blue = 0

Another Use: Modifying Variables

We need to hold on to the pixel...

...so that we can modify its attributes here.

PixelR: 255

G: 0B: 0

pixel

Page 29: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

Page 30: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

Page 31: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 32: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 33: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 34: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 35: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 36: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 37: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 38: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 39: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 40: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 41: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 42: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 43: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 44: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 45: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 46: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 47: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens?

main() says kara is super coolmain() says parth is a unicornawesome() says kara is awesomeawesome() says parth is a unicornmain() says kara is super coolmain() says parth is a unicorn

Page 48: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

main()

Page 49: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

main()

kara?

Page 50: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

main()

kara?

Page 51: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

main()

kara?

Right here!

Page 52: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

main()

parth?

Page 53: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

main()

parth?

Page 54: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

main()

parth?

Page 55: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

main()

parth?

Yeees….?

Page 56: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

Page 57: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'kara = 'fabulous'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens if we define a variable called kara at the top

level?

Page 58: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

parth = 'a unicorn'kara = 'fabulous'

def awesome(): kara = 'awesome' print('awesome() says kara is ' + kara) print('awesome() says parth is ' + parth)

def main(): kara = 'super cool' print('main() says kara is ' + kara) print('main() says parth is ' + parth) awesome() print('main() says kara is ' + kara) print('main() says parth is ' + parth)

Scope

What happens if we define a variable called kara at the top

level?

Nothing! Functions always check inside their own scope before they check the global

scope.

Page 59: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Review

Page 60: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

1. Variables! What are they good for?The broad answer: keeping track of data (you might change the data, use the data, some combination of both, etc.)

Review

Page 61: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

1. Variables! What are they good for?The broad answer: keeping track of data (you might change the data, use the data, some combination of both, etc.)

2. Functions don’t share variables.See our review session on Functions, Parameters & References Review to see how we can make functions share data and talk to each other.

Review

Page 62: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Review

1. Variables! What are they good for?The broad answer: keeping track of data (you might change the data, use the data, some combination of both, etc.)

2. Functions don’t share variables.See our review session on Functions, Parameters & References Review to see how we can make functions share data and talk to each other.

3. When you reference a variable, functions will resolve them in a specific order.

First it checks locally, then globally.

Page 63: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Arithmetic On Variables

Page 64: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

You can do some cool stuff with variables!

num = 1

num *=2num = num*2

multiplies num by 2 and reassigns num to

be that product

num +=1 num = num+1

increments num by 1 and updates num to

be the new value

num **=3num = num**3

takes the value of num, puts it to the third power and stores the value in num

Page 65: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

You can do some cool stuff with strings!

name1 = "kara"name2 = "parth"name1 *=2 → "karakara"names_together = name1+ name2 → "karakaraparth"

● You can do "multiplication" and "addition" on string variables● "multiplication" will just multiply the copies of the word you have● "addition" will add some new characters to the end of your string

(this is also called concatenation)

Page 66: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

# Returns the square root of a numbermath.sqrt(2) # => 1.4142135623730951

Libraries and Arithmetic

Page 67: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

# Returns the square root of a numbermath.sqrt(2) # => 1.4142135623730951

# Returns a random integer from range(start, stop)random.randrange(start, stop)

Libraries and Arithmetic

Page 68: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

# Returns the square root of a numbermath.sqrt(2) # => 1.4142135623730951

# Returns a random integer from range(start, stop)random.randrange(start, stop)

# Returns a random float between start and stoprandom.uniform(start, stop)

Libraries and Arithmetic

Page 69: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Variables and Control Flow

Page 70: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Using variables with if and while

● Recall that boolean expressions will evaluate to either True or False ○ 1 < 2 → True○ 'abc' == 'def' → False

● You can use the values stored in your variables in these boolean expressions! This lets you make your code more generalizable ○ if num1 < num2:

#code

Page 71: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Let's see a program!

Goal: we want to simulate a person flipping a coin. They want to get 10 heads before they stop. How can we keep track of how many tosses that takes?

1 2 3

4 5 6

7 8 9

10

Page 72: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

#a program that tosses a coin until we get 10 heads and then prints how many flips it took to get those 10 headsdef simulate_coin_toss(): num_heads = 0 num_tosses = 0 while (num_heads < 10): toss = random.randrange(0,2) num_tosses+=1 if toss == 0: num_heads +=1 print("It took " + str(num_tosses) + " to get 10 heads")

Using variables in if/while

a variable that keeps track of how many heads we've landed on so far

Page 73: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Using variables in if/while

#a program that tosses a coin until we get 10 heads and then prints how many flips it took to get those 10 headsdef simulate_coin_toss(): num_heads = 0 num_tosses = 0 while (num_heads < 10): toss = random.randrange(0,2) num_tosses+=1 if toss == 0: num_heads +=1 print("It took " + str(num_tosses) + " to get 10 heads")

a variable that keeps track of how many coin tosses there have been

Page 74: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Using variables in if/while

#a program that tosses a coin until we get 10 heads and then prints how many flips it took to get those 10 headsdef simulate_coin_toss(): num_heads = 0 num_tosses = 0 while (num_heads < 10): toss = random.randrange(0,2) num_tosses+=1 if toss == 0: num_heads +=1 print("It took " + str(num_tosses) + " to get 10 heads")

checks to see if we've landed on enough heads yet, if not, loop again!

Page 75: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Using variables in if/while

#a program that tosses a coin until we get 10 heads and then prints how many flips it took to get those 10 headsdef simulate_coin_toss(): num_heads = 0 num_tosses = 0 while (num_heads < 10): toss = random.randrange(0,2) num_tosses+=1 if toss == 0: num_heads +=1 print("It took " + str(num_tosses) + " to get 10 heads")

a variable that stores the result of our coin flip (0 for heads, 1 for tails)

Page 76: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Using variables in if/while

#a program that tosses a coin until we get 10 heads and then prints how many flips it took to get those 10 headsdef simulate_coin_toss(): num_heads = 0 num_tosses = 0 while (num_heads < 10): toss = random.randrange(0,2) num_tosses+=1 if toss == 0: num_heads +=1 print("It took " + str(num_tosses) + " to get 10 heads")

incrementing the num_toss variable to keep track of our coin toss

Page 77: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Using variables in if/while

#a program that tosses a coin until we get 10 heads and then prints how many flips it took to get those 10 headsdef simulate_coin_toss(): num_heads = 0 num_tosses = 0 while (num_heads < 10): toss = random.randrange(0,2) num_tosses+=1 if toss == 0: num_heads +=1 print("It took " + str(num_tosses) + " to get 10 heads")

checking to see if we landed on a heads or a tails

Page 78: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Using variables in if/while

#a program that tosses a coin until we get 10 heads and then prints how many flips it took to get those 10 headsdef simulate_coin_toss(): num_heads = 0 num_tosses = 0 while (num_heads < 10): toss = random.randrange(0,2) num_tosses+=1 if toss == 0: num_heads +=1 print("It took " + str(num_tosses) + " to get 10 heads")

if we landed on heads, increment the num_heads variable!

Page 79: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Using variables in if/while

#a program that tosses a coin until we get 10 heads and then prints how many flips it took to get those 10 headsdef simulate_coin_toss(): num_heads = 0 num_tosses = 0 while (num_heads < 10): toss = random.randrange(0,2) num_tosses+=1 if toss == 0: num_heads +=1 print("It took " + str(num_tosses) + " to get 10 heads")

print out how many tosses it took

Page 80: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Diagnostic Problem 4

Page 81: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Problem prompt:

Write a program that asks the user to enter a sequence of "non-decreasing" numbers one at a time. Numbers are non- decreasing if each number is greater than or equal to the last.

When the user enters a number which is smaller than their previously entered value, the program is over. Tell the user how long their sequence was.

Page 82: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num:

Page 83: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1

num1

int1

sequence_length: 0

Page 84: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

num1

int1

sequence_length: 0

print("Enter a sequence of non-decreasing numbers.)sequence_length = 0num1 = int(input('Enter a num:')

Enter a sequence of non-decreasing numbers. Enter num: 1

Page 85: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

num1

int1int2

sequence_length: 0

num2

int2

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2

Page 86: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

num1

int1int2

sequence_length: 0

num2

int2

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2

print("Enter a sequence of non-decreasing numbers.)sequence_length = 0num1 = int(input('Enter a num:')num2 = int(input('Enter a num:')

Page 87: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2

num1

int1nu

m2

int2 >=

sequence_length: 0

STOP, before asking for another number: is num2 >=

num1?

Page 88: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2

num1

int1nu

m2

int2 >=

sequence_length: 0

STOP, before asking for another number: is num2 >=

num1?

print("Enter a sequence of non-decreasing numbers.)sequence_length =0num1 = int(input('Enter a num:')num2 = int(input('Enter a num:')if num2 >= num1:

Page 89: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2

num1

int1nu

m2

int2 >=

sequence_length: 1

STOP, before asking for another number: is num2 >=

num1?Yes it is! So we ask for

another number and increment our counter variable

Page 90: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2

num1

int1nu

m2

int2 >=

sequence_length: 1

print("Enter a sequence of non-decreasing numbers.)sequence_length = 0num1 = int(input('Enter a num:')num2 = int(input('Enter a num:')if num2 >= num1:

sequence_length +=1

STOP, before asking for another number: is num2 >=

num1?Yes it is! So we ask for

another number and increment our counter variable

Page 91: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2Enter num: 3

num3

int3

num1

int1nu

m2

int2 >=

sequence_length: 1

Page 92: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2Enter num: 3

num3

int3

num1

int1nu

m2

int2 >=

sequence_length: 1

print("Enter a sequence of non-decreasing numbers.)sequence_length = 0num1 = int(input('Enter a num:')num2 = int(input('Enter a num:')if num2 >= num1:

sequence_length +=1num3 = int(input('Enter a num:'))

Page 93: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2Enter num: 3

RECONFIGURE

num1

int2nu

m2

int2

sequence_length: 1

Page 94: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2Enter num: 3

RECONFIGURE

num1

int2nu

m2

int2

sequence_length: 1

print("Enter a sequence of non-decreasing numbers.)sequence_length = 0num1 = int(input('Enter a num:')num2 = int(input('Enter a num:')if num2 >= num1:

sequence_length +=1num1 = num2

Page 95: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2Enter num: 3

num1

int2nu

m2

int3

sequence_length: 1

>=

Page 96: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2Enter num: 3

num1

int2nu

m2

int3

sequence_length: 1

>=print("Enter a sequence of non-decreasing numbers.)sequence_length = 0num1 = int(input('Enter a num:')num2 = int(input('Enter a num:')if num2 >= num1:

sequence_length +=1num1 = num2num2 = int(input('Enter a num:')

Page 97: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2Enter num: 3

num1

int2nu

m2

int3 >=

sequence_length: 2

STOP, before asking for another number: is num2 >= num1?Yes it is! So we ask for another number and increment our

counter variable

Page 98: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2Enter num: 3

num1

int2nu

m2

int3 >=

sequence_length: 2

STOP, before asking for another number: is num2 >= num1?Yes it is! So we ask for another number and increment our

counter variable

print("Enter a sequence of non-decreasing numbers.)sequence_length = 0num1 = int(input('Enter a num:')num2 = int(input('Enter a num:')while num2 >= num1:

sequence_length +=1num1 = num2num2 = int(input('Enter a num:')

Page 99: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2Enter num: 3Enter num: 4Enter num: 2

num1

int4nu

m2

int2 >=

STOP, before asking for another number: is num2 >= num1?No it isn't! So we print out the ending comments

sequence_length: 3

Page 100: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Asking for a nondecreasing sequence

Enter a sequence of non-decreasing numbers. Enter num: 1Enter num: 2Enter num: 3Enter num: 4Enter num: 2

STOP, before asking for another number: is num2 >= num1?

No it isn't! So we print out the ending comments

sequence_length: 3

print("Enter a sequence of non-decreasing numbers.)sequence_length = 0num1 = int(input('Enter a num:')num2 = int(input('Enter a num:')while num2 >= num1:

sequence_length +=1num1 = num2num2 = int(input('Enter a num:')

print ( "Thanks for playing!" ) print ( "Sequence length: " + str(sequence_length))

Page 101: CS 106A: Variables Reviewweb.stanford.edu/class/cs106a/handouts/VariablesReview.pdf · print(class_num) int 106 int 106 class_num. Okay, How Do I Use Them? Use a variable to keep

Solution

print("Enter a sequence of non-decreasing numbers.)sequence_length = 0num1 = int(input('Enter a num:')num2 = int(input('Enter a num:')while num2 >= num1:

sequence_length +=1num1 = num2num2 = int(input('Enter a num:')

print ( "Thanks for playing!" ) print ( "Sequence length: " + str(sequence_length))