Programming

MATH 230 : Fall 2023

Department of Mathematics - SUNY Geneseo
⇐ Back

Homework 2 - Booleans and if-elif-else

Due Date: September 19, 2023

Upload

Do not import any modules to solve any of the problems below. Only use what we have learned thus far. If you are not sure then please ask. When uploading your file, please make sure to click on the submit button once the file has finished uploading. Assignment due by 11:59 pm on the due date.

Problems

  1. Thus far, we have encountered the following data types:
    1. str - string data type
    2. int - integer data type
    3. float - float data type

    What new data type is introduced this week? What are the possible values for this new data type?

  2. What does the function bool() do? Give two explicit code examples and explain.
  3. What are the three boolean operators? Describe what each does and include explicit code examples for each.
  4. What are the comparison operators? Describe what each does and include explicit code examples for each.
  5. For each expression below, what is the value of the expression? Explain thoroughly.
    1. >>> 33 == '33'
    2. >>> bool(0) != bool('')
    3. >>> x = 4.3
      >>> y = 0.5
      >>> x < y or 2*y < x
    4. not (1 == '1' and 2 < 3)
  6. Write a program that asks a user to input three numbers, say \(a, b, c\), and returns a message telling the user which number entered was the "smallest", which one is the "middle" number, and which one is the "largest" number. As as an example, if the user enters \(a=3\), \(b=7\), \(c=2\) then your program should print the message:

    """The smallest number entered was 2
    The middle number entered was 3
    The largest number entered was 7
    """

    On the other hand, if the user enters two numbers that are equal, for example \(a=3\), \(b=7\), \(c=3\) then your program should output:

    """The smallest number entered was 3
    The largest number entered was 7
    """

    And if all the numbers entered are equal, for example \(a=4\), \(b=4\), \(c=4\) then your program should output:

    "All of the numbers entered are equal to 4"
    Use only if, elif, else statements; do not use lists to store your numbers and do not use functions not discussed in Chapters 1-2.
  7. You work for an airline and you are asked to create a ticket reservation system. Your airline services only two airports on the east coast, John F. Kennedy airport (JFK) and Logan International Airport (BOS). On the west coast, your airline only services Los Angeles airport (LAX). Everyday there are 5 outbound flights from the east coast to LAX as follows:

    • Three flights out of JFK leave in the morning:
      1. Lay over in Chicago with total travel time of 8 hours.
      2. Lay over in Atlanta with total travel time of 9 hours.
      3. Lay over in Kansas City with total travel time of 7 hours.
    • Two flights out of BOS leave in the afternoon:
      1. Lay over in Houston with total travel time of 10 hours.
      2. Lay over in Nashville with total travel time of 8 hours.
    To start creating the initial part of your reservation system for flights departing from the east coast, you first want to simply ask the passenger whether they prefer to depart in the morning or afternoon. Then, based on the passenger's response (either morning or afternoon), you need to present to the passenger the options available to them. Once the passenger inputs their second choice that determines the connecting flight, you need to summarize their flight details and then ask the passenger whether to go ahead and make the reservation. If the passenger says yes, then display a message to the passenger of the type:
    "Please have your credit card ready to purchase your flight."
    If the passenger says no then display a message to the passenger of the type:
    "Would you like to start again with a new reservation?"
    Be creative!