Python Crash Course - Chapter 7: User Input and while Loops

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What does a continue statement do?

A continue statement will return to the beginning of the loop based on the result of a conditional test. The continue statement tells Python to ignore the rest of the loop and return to the beginning. current_number = 0 while current_number < 10: <<tab>>current_number += 1 <<tab>>if current_number % 2 == 0: <<tab>><<tab>>continue <<tab>>print(current_number) >>> 1 >>> 3 >>> 5 >>> 7 >>> 9

What is a flag in Python?

A flag is one variable that acts as a signal to the program. It determines whether or not the entire program is active.

What is a way to move an item from one list to another?

Begin by using a while loop. Create a variable to store a popped item from the first list. Next, append the popped item to the second list. list_one = ['dog','cat','bird'] list_two = [] while list_one: <<tab>><<tab>>item = list_one.pop() <<tab>><<tab>>print("Moving item " + item) <<tab>><<tab>>list_two.append(item)

What is a break statement?

The break statement directs the flow of your program; you can use it to control which lines of code are executed and which aren't, so the program only executes code that you want it to, when you want it to. In other words, use to exit a while loop immediately without running any remaining code in the loop, recardless of the results of any conditional test. while True: <<tab>>city = raw_input(prompt) <<tab>>if city == 'quit': <<tab>><<tab>>print("Bye.\n") <<tab>><<tab>>break <<tab>>else: <<tab>><<tab>>print("I'd love to go to " + city.title() + "!")

What does the int() function do?

The int() function tells Python to treat the input as a numerical value rather than as a string. This is important when the value is needed for math or comparisons with other numbers. age = raw_input("How old are you " + name + "? ") age = int(age) print(age) >>> 40 (i.e. whatever number you enter)

What does the modulo operator(%) do?

The modulo operator(%) divides one number by another number and returns the remainder. In other words, the modulo operator (%) doesn't tell you how many times one number fits into another; it just tells you what the remainder is.

What does the operator '+=' do?

The operator '+=' takes a string that is stored in a variable and adds a new string onto the end.

What does a while loop do?

The while loop runs as long as, or while, a certain condition is true. In contrast, the for loop takes a collection of items and executes a block of code once for each item in the collection.

How would you use a while loop to remove all occurrences of an item from a list?

Use the remove() function in a while loop to delete all instances of an item from a list. pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat'] print("Exterminate the cats:") while 'cat' in pets: <<tab>><<tab>>pets.remove('cat')

What does the input() function do?

When you use the input() function (or raw_input() in Python 2.7), Python interprets everything the user enters as a string. name = raw_input("What's your name? ") print(name.title()) >>> John (i.e. whatever you enter)

When working with lists and dictionaries to collect, store, and organize lots of input, what type of loop should you use?

You should use a while loop with lists and dictionaries to collect, store, and organize lots of input.

Why shouldn't you modify a list inside a for loop?

You shouldn't modify a list inside a for loop because Python will have trouble keeping track of the items in the list. To modify a list as you work through it, use a while loop.


Kaugnay na mga set ng pag-aaral

Anatomy Lecture Final Exam: The Digestive System

View Set

Business management Honors State Test Review 3.00

View Set

Intro to Cybersecurity CIT 171 REVIEW

View Set

Management Ch. 1 General Nature of Management

View Set

Chapter 2: Collecting Subjective Data: The Interview and Health History - ML4

View Set