Menu
Home
Log in / Register









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

2.10 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.

2.10.1 Solutions to Practice Problem 2.1

1 month = in p ut (" Please enter a month :")

2 i f month == " December ":

3 p rint (" Merry Christmas !")

4 p rint (" Have a Happy New Year !")

2.10.2 Solutions to Practice Problem 2.2

1 month = in p ut (" Please enter a month :")

2 i f month == " December ":

3 p rint (" Merry Christmas !")

4 e l s e :

5 p rint (" You ' ll have to wait ")

6 p rint (" Have a Happy New Year !")

2.10.3 Solutions to Practice Problem 2.3

1 sideone = int ( in p ut (

2 " Please enter length of shortest side of triangle :" ))

3 sidetwo = int ( in p ut (

4 " Please enter length of middle side of triangle :" ))

5 sidethree = int ( in p ut (

6 " Please enter length of longest side of triangle :" ))

7

8 msg =" It is a perfect triangle ."

9

10 i f sideone % 3 != 0:

11 msg = " It is not a perfect triangle ."

12

13 i f sidetwo % 4 != 0:

14 msg = " It is not a perfect triangle ."

15

16 i f sidethree % 5 != 0:

17 msg = " It is not a perfect triangle ."

18

19 i f sideone ** 2 + sidetwo ** 2 != sidethree **2:

20 msg = " It is not a perfect triangle ."

21 p rint ( msg )

2.10.4 Solutions to Practice Problem 2.4

1 month = in p ut (" Please enter a month :")

2 i f month == " January ":

3 msg =" Hello Snow !"

4 e l i f month == " February ":

5 msg =" More Snow !"

6 e l i f month == " March ":

7 msg =" No More Snow !"

8 e l i f month == " April ":

9 msg =" Almost Golf Time "

10 e l i f month == " May ":

11 msg =" Time to Golf "

12 e l i f month == " June ":

13 msg =" School 's Out "

14 e l i f month == " July ":

15 msg =" Happy Fourth "

16 e l i f month == " August ":

17 msg =" Still Golfing "

18 e l i f month == " September ":

19 msg =" Welcome Back !"

20 e l i f month == " October ":

21 msg =" Fall Colors "

22 e l i f month == " November ":

23 msg =" Turkey Day "

24 e l i f month == " December ":

25 msg =" Merry Christmas !"

26 e l s e :

27 msg =" You entered an incorrect month ."

28

29 p rint ( msg )

2.10.5 Solutions to Practice Problem 2.5

1 age = int ( in p ut (" Please enter your age :" ))

2 resident = in p ut (

3 " Are you a natural born citizen of the U. S. ( yes / no )? ")

4 years = int ( in p ut (

5 " How many years have you resided in the U. S .? " ))

6

7 eligible = True

8 i f age < 35:

9 eligible = False

10

11 i f resident != " yes ":

12 eligible = False

13

14 i f years < 14 :

15 eligible = False

16

17 i f eligible :

18 p rint (" You can run for president !")

19 e l s e :

20 p rint (" You are not eligible to run for president !")

2.10.6 Solutions to Practice Problem 2.6

1 age = int ( in p ut (" What is your age ?" ))

2 license = in p ut (

3 " Do you have a fishing license in MN ( yes / no )? ")

4 parentlic = in p ut (

5 " Does your parent have a fishing license ( yes / no )? ")

6

7 i f ( age < 16 a n d parentlic == " yes ") or license == " yes ":

8 p rint (" You are legal to fish in MN .")

9 e l s e :

10 p rint (" You are not legal to fish in MN .")

2.10.7 Solutions to Practice Problem 2.7

1 sideone = f l o a t ( in p ut (

2 " Please enter length of shortest side of triangle :" ))

3 sidetwo = f l o a t ( in p ut (

4 " Please enter length of middle side of triangle :" ))

5 sidethree = f l o a t ( in p ut (

6 " Please enter length of longest side of triangle :" ))

7

8 ratio = sideone / 3

9

10 msg =" It is a perfect triangle ."

11

12 i f a bs (( ratio sidetwo / 4) / sidetwo ) > 0.001:

13 msg =" It is not a perfect triangle ."

14

15 i f a bs (( ratio sidethree / 5) / sidethree ) > 0.001:

16 msg =" It is not a perfect triangle ."

17

18 p rint ( msg )

2.10.8 Solutions to Practice Problem 2.8

1 try :

2 age = int ( in p ut (" What is your age ?" ))

3 exce p t :

4 p rint (" You did not enter your age correctly .")

5 exit ( 0 )

6

7 license = in p ut (

8 " Do you have a fishing license in MN ( yes / no )? ")

9 parentlic = in p ut (

10 " Does your parent have a fishing license ( yes / no )? ")

11

12 i f ( age < 16 a n d parentlic == " yes ") or license == " yes ":

13 p rint (" You are legal to fish in MN .")

14 e l s e :

15 p rint (" You are not legal to fish in MN .")

 
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