If Statement Exercises
The exercises in this chapter will require you to use decision making constructs so that your programs can handle a variety of different situations.
- PDF / 131,087 Bytes
- 13 Pages / 439.37 x 666.142 pts Page_size
- 85 Downloads / 372 Views
If Statement Exercises
The programming constructs that you used to solve the exercises in the previous chapter will continue to be useful as you tackle these problems. In addition, the exercises in this chapter will require you to use decision making constructs so that your programs can handle a variety of different situations that might arise. You should expect to use some or all of these Python features when completing these problems: • • • •
Make a decision with an if statement Select one of two alternatives with an if-else statement Select from one of several alternatives by using an if-elif or if-elif-else statement Construct a complex condition for an if statement that includes the Boolean operators and, or and not • Nest an if statement within the body of another if statement
Exercise 34: Even or Odd? (Solved—13 Lines) Write a program that reads an integer from the user. Then your program should display a message indicating whether the integer is even or odd.
Exercise 35: Dog Years (22 Lines) It is commonly said that one human year is equivalent to 7 dog years. However this simple conversion fails to recognize that dogs reach adulthood in approximately two years. As a result, some people believe that it is better to count each of the first two human years as 10.5 dog years, and then count each additional human year as 4 dog years. © Springer International Publishing Switzerland 2014 B. Stephenson, The Python Workbook, DOI 10.1007/978-3-319-14240-1_2
15
16
2 If Statement Exercises
Write a program that implements the conversion from human years to dog years described in the previous paragraph. Ensure that your program works correctly for conversions of less than two human years and for conversions of two or more human years. Your program should display an appropriate error message if the user enters a negative number.
Exercise 36: Vowel or Consonant (Solved—16 Lines) In this exercise you will create a program that reads a letter of the alphabet from the user. If the user enters a, e, i, o or u then your program should display a message indicating that the entered letter is a vowel. If the user enters y then your program should display a message indicating that sometimes y is a vowel, and sometimes y is a consonant. Otherwise your program should display a message indicating that the letter is a consonant.
Exercise 37: Name that Shape (Solved—31 Lines) Write a program that determines the name of a shape from its number of sides. Read the number of sides from the user and then report the appropriate name as part of a meaningful message. Your program should support shapes with anywhere from 3 up to (and including) 10 sides. If a number of sides outside of this range is entered then your program should display an appropriate error message.
Exercise 38: Month Name to Number of Days (Solved—18 Lines) The length of a month varies from 28 to 31 days. In this exercise you will create a program that reads the name of a month from the user as a string. Then your program should display the number of days in tha
Data Loading...