acm init() day 3

55
ACM init() Day 3: April 29, 2015

Upload: ucla-association-of-computing-machinery

Post on 03-Aug-2015

105 views

Category:

Education


1 download

TRANSCRIPT

Page 1: ACM init() Day 3

ACM init()Day 3: April 29, 2015

Page 2: ACM init() Day 3

From last time

• Input- Madlibs!

• Comparison operators

• Boolean operators

• Conditionals

Page 3: ACM init() Day 3

The calculator

Page 4: ACM init() Day 3

Can we improve this• This calculator is pretty inconvenient

• User must be prompted often

• Unfriendly interface

• Only one calculation can be performed

• Can this be improved?

Page 5: ACM init() Day 3

Of course we can!We can use 'loops' to make a much better calculator

Page 6: ACM init() Day 3

Why is this better?

• We can do more than one calculation

• The user can type in a legitimate mathematical equation

• There is less confusion and clutter in the interface

Page 7: ACM init() Day 3

Loops

• Loops are a great tool if you want to repeat an action

• We are going to go over two types of loops: 'while' and 'for'

Page 8: ACM init() Day 3

While loopA while loop checks to see if a certain condition is true, and while it is, the loop keeps running. As soon as the condition

stops being true, the loop stops.

while statement is true: do something

Page 9: ACM init() Day 3

While example

While loop! The indents are important. They tell Python what is still part of the loop.

Page 10: ACM init() Day 3

While example

x = 1

Output:

Page 11: ACM init() Day 3

While example

x = 1

Output:

Page 12: ACM init() Day 3

While example

x = 1

Output: 1

Page 13: ACM init() Day 3

While example

x = 2

Output: 1

Page 14: ACM init() Day 3

While example

x = 2

Output: 1

Page 15: ACM init() Day 3

While example

x = 2

Output: 1 2

Page 16: ACM init() Day 3

While example

x = 3

Output: 1 2

Page 17: ACM init() Day 3

While example

x = 3

Output: 1 2

Page 18: ACM init() Day 3

While example

x = 3

Output: 1 2 3

Page 19: ACM init() Day 3

While example

x = 4

Output: 1 2 3

Page 20: ACM init() Day 3

While example

x = 4

Output: 1 2 3

Page 21: ACM init() Day 3

While example

x = 4

Output: 1 2 3

END

Page 22: ACM init() Day 3

Another while example

This will print out the entire song!

Page 23: ACM init() Day 3

Quick aside

str(x) will convert a number to a string. This allows you to concatinate numbers and strings together when printing.

Page 24: ACM init() Day 3

DangerWe always have a condition that will make the loop end.

What if we forget to update the condition? !

We might create something called an infinite loop. This loop will keep running forever, which is really bad.

Page 25: ACM init() Day 3

Infinite Loop OutputAHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH

...

Page 26: ACM init() Day 3

Infinte loops

Infinite loops are bad because they keep going forever, might crash your computer, and are generally annoyin. Be careful!

Page 27: ACM init() Day 3

BreakPut 'break' anywhere in a while loop to instantly leave the loop.

Page 28: ACM init() Day 3

While-else

This is similar to if-else, with an exception! The else in a while loop will execute when the loop condition becomes false. It will

not execute if break is used.

Here's an example: we're going to generate 3 random numbers. If one of them is a 5, you lose! Otherwise, you win.

Page 29: ACM init() Day 3
Page 30: ACM init() Day 3

ChallengeWe're going to make a game similar to the example on the last

slide. However, allow the user to guess what the number is three times.

Remember, raw_input turns user input into a string, so we use int() to make it a number again.

Page 31: ACM init() Day 3

Use a while loop to let the user keep guessing so long as guesses_left is greater than zero.

!Ask the user for their guess.

!If they guess correctly, print "You win!" and break.

!Decrement guesses_left by one.

!Use an else: case after your while loop to print "You lose."

Here's a start!

Page 32: ACM init() Day 3
Page 33: ACM init() Day 3

A brief sidenoteou might have seen that in loops I was using += and -=

These are just shorthand.

Page 34: ACM init() Day 3

For loopA 'for' loop is used when we know how many times we'll be

looping when we start. The loop will run however many times we tell it to.

for variable in range(number): do something

Page 35: ACM init() Day 3

For example

The range is from 0 to 3, not including 3!

Page 36: ACM init() Day 3

For example

i = 0

Output:

Page 37: ACM init() Day 3

For example

i = 0

Output: 0

Page 38: ACM init() Day 3

For example

i = 1

Output: 0

Page 39: ACM init() Day 3

For example

i = 1

Output: 0 1

Page 40: ACM init() Day 3

For example

i = 2

Output: 0 1

Page 41: ACM init() Day 3

For example

i = 2

Output: 0 1 2

Page 42: ACM init() Day 3

For example

i = 3

Output: 0 1 2

Page 43: ACM init() Day 3

For example

i = 3

Output: 0 1 2END

Page 44: ACM init() Day 3

What will this print?

Page 45: ACM init() Day 3

Answer1 2 3 4 5 6 7 8 9

Page 46: ACM init() Day 3

We can loop through strings too!

This will print out every letter in the string.

Page 47: ACM init() Day 3

Problem

Write a for loop to print out every letter in any word a user types in.

Page 48: ACM init() Day 3

Answer

Page 49: ACM init() Day 3

What will this print out?

Page 50: ACM init() Day 3

Answer

1 3 5 7 9

The continue keyword moves on the the next iteration of the loop.

Page 51: ACM init() Day 3

Challenge problem: Fizzbuzz

Write a program that prints the numbers from 1 to 100. For multiples of three print “Fizz” instead of the number and for

the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

The next slide shows what the output should look like for the first 26 numbers.

Page 52: ACM init() Day 3
Page 53: ACM init() Day 3

Fizzbuzz!

Page 54: ACM init() Day 3

What we did today• Loops!

• While loop- don't make them infinite!

• While-else

• For loops

• For-else loops exist as well

Page 55: ACM init() Day 3

Calculator revisitedUsing the loops from today try to improve your calculator. Hint:

you'll probably have to use both types of loops! !

Another hint: if you want to access a certain character of a string you can do it like this:

string[i] = ... i is the index of the string, starting at 0.