SlideShare uma empresa Scribd logo
1 de 55
Baixar para ler offline
ACM init()
Day 3: April 29, 2015
From last time
• Input- Madlibs!
• Comparison operators
• Boolean operators
• Conditionals
The calculator
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?
Of course we can!
We can use 'loops' to make a much better calculator
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
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'
While loop
A 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
While example
While loop! The indents are important. They tell Python what is
still part of the loop.
While example
x = 1
Output:
While example
x = 1
Output:
While example
x = 1
Output:
1
While example
x = 2
Output:
1
While example
x = 2
Output:
1
While example
x = 2
Output:
1
2
While example
x = 3
Output:
1
2
While example
x = 3
Output:
1
2
While example
x = 3
Output:
1
2
3
While example
x = 4
Output:
1
2
3
While example
x = 4
Output:
1
2
3
While example
x = 4
Output:
1
2
3
END
Another while example
This will print out the entire song!
Quick aside
str(x) will convert a number to a string. This allows you to
concatinate numbers and strings together when printing.
Danger
We 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.
Infinite Loop Output
AHHHHHHHHHHHHHHHHHHHHH
AHHHHHHHHHHHHHHHHHHHHH
AHHHHHHHHHHHHHHHHHHHHH
AHHHHHHHHHHHHHHHHHHHHH
AHHHHHHHHHHHHHHHHHHHHH
AHHHHHHHHHHHHHHHHHHHHH
AHHHHHHHHHHHHHHHHHHHHH
AHHHHHHHHHHHHHHHHHHHHH
AHHHHHHHHHHHHHHHHHHHHH
AHHHHHHHHHHHHHHHHHHHHH
AHHHHHHHHHHHHHHHHHHHHH
AHHHHHHHHHHHHHHHHHHHHH
...
Infinte loops
Infinite loops are bad because they keep going forever, might
crash your computer, and are generally annoyin. Be careful!
Break
Put 'break' anywhere in a while loop to instantly leave the loop.
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.
Challenge
We'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.
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!
A brief sidenote
ou might have seen that in loops I was using += and -=
These are just shorthand.
For loop
A '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
For example
The range is from 0 to 3, not including 3!
For example
i = 0
Output:
For example
i = 0
Output:
0
For example
i = 1
Output:
0
For example
i = 1
Output:
0
1
For example
i = 2
Output:
0
1
For example
i = 2
Output:
0
1
2
For example
i = 3
Output:
0
1
2
For example
i = 3
Output:
0
1
2
END
What will this print?
Answer
1
2
3
4
5
6
7
8
9
We can loop through strings
too!
This will print out every letter in the string.
Problem
Write a for loop to print out every letter in any word a user
types in.
Answer
What will this print out?
Answer
1
3
5
7
9
The continue keyword moves on the the next iteration of the
loop.
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.
Fizzbuzz!
What we did today
• Loops!
• While loop- don't make them infinite!
• While-else
• For loops
• For-else loops exist as well
Calculator revisited
Using 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.

Mais conteúdo relacionado

Semelhante a ACM init() Day 3

Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3aRuth Marvin
 
Little lessons learned from Swift
Little lessons learned from SwiftLittle lessons learned from Swift
Little lessons learned from SwiftPablo Villar
 
This is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptxThis is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptxelezearrepil1
 
C++ Course - Lesson 1
C++ Course - Lesson 1C++ Course - Lesson 1
C++ Course - Lesson 1Mohamed Ahmed
 
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops  General Discussion of Loops A loop is a.docxC++ Loops  General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docxhumphrieskalyn
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docxJavvajiVenkat
 
Programming methodology lecture07
Programming methodology lecture07Programming methodology lecture07
Programming methodology lecture07NYversity
 
Dear compiler please don't be my nanny v2
Dear compiler  please don't be my nanny v2Dear compiler  please don't be my nanny v2
Dear compiler please don't be my nanny v2Dino Dini
 
ICPSR - Complex Systems Models in the Social Sciences - Lab Session 6 - Profe...
ICPSR - Complex Systems Models in the Social Sciences - Lab Session 6 - Profe...ICPSR - Complex Systems Models in the Social Sciences - Lab Session 6 - Profe...
ICPSR - Complex Systems Models in the Social Sciences - Lab Session 6 - Profe...Daniel Katz
 
Python if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementPython if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementAbhishekGupta692777
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxmonicafrancis71118
 
Back to Basics 3: Scaling 30,000 Requests a Second with MongoDB
Back to Basics 3: Scaling 30,000 Requests a Second with MongoDBBack to Basics 3: Scaling 30,000 Requests a Second with MongoDB
Back to Basics 3: Scaling 30,000 Requests a Second with MongoDBMongoDB
 

Semelhante a ACM init() Day 3 (20)

Loops
LoopsLoops
Loops
 
While loop
While loopWhile loop
While loop
 
Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3a
 
Little lessons learned from Swift
Little lessons learned from SwiftLittle lessons learned from Swift
Little lessons learned from Swift
 
This is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptxThis is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptx
 
ACM init() Day 2
ACM init() Day 2ACM init() Day 2
ACM init() Day 2
 
C++ Course - Lesson 1
C++ Course - Lesson 1C++ Course - Lesson 1
C++ Course - Lesson 1
 
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops  General Discussion of Loops A loop is a.docxC++ Loops  General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docx
 
ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1
 
binary_logic.pptx
binary_logic.pptxbinary_logic.pptx
binary_logic.pptx
 
Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Small Basic - Branching and Loop
Small Basic - Branching and LoopSmall Basic - Branching and Loop
Small Basic - Branching and Loop
 
Programming methodology lecture07
Programming methodology lecture07Programming methodology lecture07
Programming methodology lecture07
 
Dear compiler please don't be my nanny v2
Dear compiler  please don't be my nanny v2Dear compiler  please don't be my nanny v2
Dear compiler please don't be my nanny v2
 
ICPSR - Complex Systems Models in the Social Sciences - Lab Session 6 - Profe...
ICPSR - Complex Systems Models in the Social Sciences - Lab Session 6 - Profe...ICPSR - Complex Systems Models in the Social Sciences - Lab Session 6 - Profe...
ICPSR - Complex Systems Models in the Social Sciences - Lab Session 6 - Profe...
 
Python if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementPython if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_Statement
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
 
Cat scratch
Cat scratchCat scratch
Cat scratch
 
Back to Basics 3: Scaling 30,000 Requests a Second with MongoDB
Back to Basics 3: Scaling 30,000 Requests a Second with MongoDBBack to Basics 3: Scaling 30,000 Requests a Second with MongoDB
Back to Basics 3: Scaling 30,000 Requests a Second with MongoDB
 

Mais de UCLA Association of Computing Machinery (13)

ACM init()- Day 4
ACM init()- Day 4ACM init()- Day 4
ACM init()- Day 4
 
UCLA ACM Spring 2015 general meeting
UCLA ACM Spring 2015 general meetingUCLA ACM Spring 2015 general meeting
UCLA ACM Spring 2015 general meeting
 
UCLA Geek Week - Claim Your Domain
UCLA Geek Week - Claim Your DomainUCLA Geek Week - Claim Your Domain
UCLA Geek Week - Claim Your Domain
 
Intro to Hackathons (Winter 2015)
Intro to Hackathons (Winter 2015)Intro to Hackathons (Winter 2015)
Intro to Hackathons (Winter 2015)
 
An Introduction to Sensible Typography
An Introduction to Sensible TypographyAn Introduction to Sensible Typography
An Introduction to Sensible Typography
 
Building a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground UpBuilding a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground Up
 
ACM init() Day 6
ACM init() Day 6ACM init() Day 6
ACM init() Day 6
 
ACM init() Day 5
ACM init() Day 5ACM init() Day 5
ACM init() Day 5
 
Init() Day 4
Init() Day 4Init() Day 4
Init() Day 4
 
Init() Lesson 2
Init() Lesson 2Init() Lesson 2
Init() Lesson 2
 
ACM Fall General Meeting
ACM Fall General Meeting ACM Fall General Meeting
ACM Fall General Meeting
 
ACM Teach - Hackathon Tips and Tricks - Spring 2014
ACM Teach - Hackathon Tips and Tricks - Spring 2014ACM Teach - Hackathon Tips and Tricks - Spring 2014
ACM Teach - Hackathon Tips and Tricks - Spring 2014
 
ACM General Meeting - Spring 2014
ACM General Meeting - Spring 2014ACM General Meeting - Spring 2014
ACM General Meeting - Spring 2014
 

Último

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 

Último (20)

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

ACM init() Day 3