90 likes | 102 Views
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) – Exercises. Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu. Objectives. In this chapter, you will do some exercises about: basic control structures in Python.
E N D
CSCI/CMPE 4341 Topic: Programming in PythonChapter 4: Control Structures(Part 2) – Exercises Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu
Objectives • In this chapter, you will do some exercises about: • basic control structures in Python
Multiple Choices • Typically, ______ statements are used for counter-controlled repetition. • A. for B. while C. repetition D. until • Typically, ______ statements are used for sentinel-controlled repetition. • A. for B. while C. repetition D. until • The _________statement, when executed in a repetition statement, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. • A. break B. exit C. continue D. return • The _____operator can be used to ensure that two conditions are both true before choosing a certain path of execution • A. and B. && C. or D. || • An infinite loop occurs when the loop-continuation condition in a while statement______. • A. never becomes true B. never becomes false C. is false D. is true for finite times
Multiple Choices (cont'd) • What is the range of function call range(1, 10)? • A. [0, 10] B. [0, 10) C. [1, 10] D. [1, 10) • What is the value of the expression of 4/5*2+2**(1+2)? • A. 8 B. 10 C. 9.6 D. 8.4 • What is the value of 5/4, 5//4, and 5%4? • A. 1, 1.25, 1 B. 1.25, 1, 4 C. 1, 1, 1 D. 1.25, 1, 1 • Which of the following is the appropriate for statement for varying the control variable over the following sequence of values: 5, 10, 15, 20, 25? • A. for i in range(25): B. for i in range (5, 25): • C. for i in range(5, 25, 5): D. for i in range(25, 5, -5):
True/False Statements • The exponentiation operator ** associates left to right. • Function call range (4, 7) returns the sequence 4 to 7, inclusive. • Sentinel-controlled repetition uses a counter variable to control the number of times a set of instructions executes. • String format specifier "%5d" outputs integer with right alignment by default. • The expression ((x>y) and (a<b)) is True if either (x>y) is True or (a<b) is True. • An expression containing the or operator is True if either or both of its operands are True. • The and operator has a higher precedence than the or operator.
What Does the Code Do? for i in range(1, 11): for j in range (1, 11): print ('@', end = ""); print ("");
Debug Errors counter=1 For i in range (1, 10) counter++
Write a Program • Use nested for statement to write a Python program to display the triangle of asterisks. Use the following statements: • print ("*", end="") # displays asterisks one at a time • print ("") # the cursor goes to a new line • print (" ", end="") # inserts a space * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Write a Program (cont'd) • Use nested for statement to write a program to display a rectangle. Use the following statements: • print ("+", end="") • print ("-", end="") • print ("|", end="") • print ("") • print (" ", end="") + - - - - - - - - - - + | | | | | | + - - - - - - - - - - +