Menu
Home
Log in / Register















 
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

3.11 Solutions to Practice Problems

These are solutions to the practice problems in this chapter. You should only consult these answers after you have tried each of them for yourself first. Practice problems are meant to help reinforce the material you have just read so make use of them.

3.11.1 Solutions to Practice Problem 3.1

sentence = input (" Please enter a sentence : ")

print (" Here is the sentence with the case swapped .")

print ( sentence . swapcase ())

3.11.2 Solutions to Practice Problem 3.2

answer = input (" Please answer yes or no : ")

i f n o t answer . islower ():

print (" Next time please user all lower case letters .")

3.11.3 Solutions to Practice Problem 3.3

The else would be optional for this exercise.

1 answer = input (" Please answer yes or no : ")

2 i f answer . lower ()== " yes ":

3 print (" You entered yes .")

4 e l i f answer . lower ()== " no ":

5 print (" You answered no .")

6 e l s e :

7 print (" You answered neither yes or no .")

3.11.4 Solutions to Practice Problem 3.4

1. Does the string s change as the code is executed? No it does not.

2. What happens if the user just presses enter when prompted instead of typing any characters?

The body of the for loop is not executed at all.

3.11.5 Solutions to Practice Problem 3.5

s = input (" Please type some characters and press enter :")

for c in s:

print ( c. upper ())

3.11.6 Solutions to Practice Problem 3.6

for i in r a n g e (5):

print (i)

3.11.7 Solutions to Practice Problem 3.7

s = input (" Please type some characters and press enter :")

for i in r a n g e ( len ( s ) -1 , -1 , -1):

print (s[i ])

3.11.8 Solutions to Practice Problem 3.8

The split method returns a list of strings. The for loop iterates over the list. Each time through the loop the word variable is referencing the next string in the list.

3.11.9 Solutions to Practice Problem 3.9

If the containsEven if statement were indented, then the for loop would check to see if containsEven were true or false each time through the loop. The program would print that the list did not contain an even number (even though it might) over and over again until an even number was found. Then it would print it did contain an even number over and over again. It would print one line for each element of the list.

3.11.10 Solutions to Practice Problem 3.10

1 entry = input (" Please make your blog entry for today : ")

2 found = False

3 for word in entry . split ():

4 i f word in [ ' orderly ',' shopping ',' repeat ',' again ',

5 ' gamble ',' bid ']:

6 found = True

7

8 i f found :

9 print (" You really need to talk to somone about this .")

10 e l s e :

11 print (" Thanks for you entry .")

3.11.11 Solutions to Practice Problem 3.11

If the question variable were printed it would be the same as if the answer variable were printed. Both question and answer refer to the same list.

3.11.12 Solutions to Practice Problem 3.12

1 s = input (" Please enter a list of integers :")

2

3 lst = s. split () # Now lst is a list of strings .

4

5 count = 0 # Here is the beginning of the accumulator pattern

6

7 for e in lst :

8 i f i n t ( e) % 2 == 0:

9 count = count + 1

10

11 print (" There were " , count ," even integers in the list .")

3.11.13 Solutions to Practice Problem 3.13

1 n = i n t ( input (" Please enter a non negative integer : " ))

2

3 factorial = 1

4 for i in r a n g e (1 , n +1):

5 factorial = factorial * i

6

7 print ( s t r ( n )+ "! =" , factorial )

3.11.14 Solutions to Practice Problem 3.14

1 filename = input (" Please enter the name of a file :")

2 catfile = open ( filename ,"r")

3 for line in catfile :

4 print ( line . rstrip ())

5 catfile . close ()

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel