Write a program that allows a user to enter a string containing a day of the week ("Sunday," "Monday," "Tuesday," etc.) and uses a switch construct to convert the day to its corresponding number, where Sunday is considered the first day of the week and Saturday is considered the last day of the week. Print out the resulting day number. Also, be sure to handle the case of an illegal day name! (Note: Be sure to use the 's' option on function input so that the input is treated as a string.)

Short Answer

Expert verified
To create a program that converts a day of the week (as a string) to its corresponding number using a switch construct, follow these steps: 1. Get user input for the day of the week and store it as a string variable. 2. Convert the string input to lowercase for easy comparison. 3. Create a switch construct using a dictionary to compare the input with each day of the week and assign the corresponding number. 4. Include a case for handling invalid input and print an error message. 5. Print the day number. Example code in Python: ```python def main(): day = input("Enter a day of the week: ").strip().lower() switch = { "sunday": 1, "monday": 2, "tuesday": 3, "wednesday": 4, "thursday": 5, "friday": 6, "saturday": 7 } day_number = switch.get(day, 'invalid') if day_number == 'invalid': print("Invalid day name!") else: print("The corresponding number is:", day_number) if __name__ == "__main__": main() ``` Note: Python uses a dictionary as an alternative to a native switch construct.

Step by step solution

01

Get user input for the day of the week

Ask the user to enter a day of the week and store it as a string variable.
02

Convert the string input to lowercase

Convert the given input to lowercase so that it is easier to compare and manage the cases in the switch construct (e.g. "monday", "Monday", "MONDAY" will all be the same).
03

Create a switch construct

Set up a switch construct to compare the input with each day of the week (in lowercase) and assign the corresponding number for each day, where Sunday is 1 and Saturday is 7.
04

Case for invalid input

In the switch construct, also create a case for handling invalid input, which will print an error message to inform the user that the input is not a valid day name.
05

Print the day number

Print out the day number/result of the switch construct. Here's an example code for this program in Python: ```python def main(): day = input("Enter a day of the week: ").strip().lower() switch = { "sunday": 1, "monday": 2, "tuesday": 3, "wednesday": 4, "thursday": 5, "friday": 6, "saturday": 7 } day_number = switch.get(day, 'invalid') if day_number == 'invalid': print("Invalid day name!") else: print("The corresponding number is:", day_number) if __name__ == "__main__": main() ``` Note that Python doesn't have a native switch construct, so we use a dictionary as an alternative.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

When a ray of light passes from a region with an index of refraction \(n_{1}\) into a region with a different index of refraction \(n_{2}\), the light ray is bent (see Figure 3.16). The angle at which the light is bent is given by Snell's law. $$ n_{1} \sin \theta_{1}=n_{2} \sin \theta_{2} $$ where \(\theta_{1}\) is the angle of incidence of the light in the first region, and \(\theta_{2}\) is the angle of incidence of the light in the second region. Using Snell's law, it is possible to predict the angle of incidence of a light ray in Region 2 if the angle of incidence \(\theta_{1}\) in Region \(I\) and the indices of refraction \(n_{1}\) and \(n_{2}\) are known. The equation to perform this calculation is $$ \theta_{2}=\sin ^{-1}\left(\frac{n_{1}}{n_{2}} \sin \theta_{1}\right) $$ Write a program to calculate the angle of incidence (in degrees) of a light ray in Region 2 given the angle of incidence \(\theta_{1}\) in Region \(I\) and the indices of refraction \(n_{1}\) and \(n_{2}\). (Note: If \(n_{1}>n_{2}\), then for some angles \(\theta_{1}\). Equation \(3.8\) will have no real solution because the absolute value of the quantity \(\left(\frac{n_{1}}{n_{2}} \sin \theta_{1}\right)\) will be greater than 1.0. When this occurs, all light is reflected back into Region 1, and no light passes into Region 2 at all. Your program must be able to recognize and properly handle this condition.) The program should also create a plot showing the incident ray, the boundary between the two regions, and the refracted ray on the other side of the boundary. Test your program by running it for the following two cases: \((a) n_{1}=\) 1. \(0, n_{2}=1.7\), and \(\theta_{1}=45^{\circ},(b) n_{1}=1.7, n_{2}=1.0\), and \(\theta_{1}=45^{\circ}\).

The Spiral of Archimedes. The spiral of Archimedes is a curve described in polar coordinates by the equation $$ r=k \theta $$ where \(r\) is the distance of a point from the origin, and \(\theta\) is the angle of that point in radians with respect to the origin. Plot the spiral of Archimedes for \(0 \leq \theta \leq 6 \pi\) when \(k=0.5\). Be sure to label your plot properly.

The tangent function is defined as \(\tan \theta=\sin \theta / \cos \theta\). This expression can be evaluated to solve for the tangent as long as the magnitude of \(\cos\) \(\theta\) is not too near to 0 . (If \(\cos \theta\) is 0 , evaluating the equation for \(\tan \theta\) will produce the non-numerical value Inf.) Assume that \(\theta\) is given in degrees, and write the MATLAB statements to evaluate \(\tan \theta\) as long as the magnitude of \(\cos \theta\) is greater than or equal to \(10^{-20}\). If the magnitude of \(\cos \theta\) is less than \(10^{-20}\), write out an error message instead.

In Example 3.3, we wrote a program to evaluate the function \(f(x, y)\) for any two user-specified values \(x\) and \(y\). where the function \(f(x, y)\) was defined as follows. $$ f(x, y)=\left\\{\begin{array}{llll} x+y & x \geq 0 & \text { and } & y \geq 0 \\ x+y^{2} & x \geq 0 & \text { and } & y<0 \\ x^{2}+y & x<0 & \text { and } & y \geq 0 \\ x^{2}+y^{2} & x<0 & \text { and } & y<0 \end{array}\right. $$ The problem was solved by using a single if construct with 4 code blocks to calculate \(f(x, y)\) for all possible combinations of \(x\) and \(y\). Rewrite program funxy to use nested if constructs, where the outer construct evaluates the value of \(x\) and the inner constructs evaluate the value of \(y\).

Plotting Orbits When a satellite orbits the Earth, the satellite's orbit will form an ellipse with the Earth located at one of the focal points of the ellipse. The satellite's orbit can be expressed in polar coordinates as $$ r=\frac{p}{1-\varepsilon \cos \theta} $$ where \(r\) and \(\theta\) are the distance and angle of the satellite from the center of the Earth, \(p\) is a parameter specifying the size of the orbit, and \(\varepsilon\) is a parameter representing the eccentricity of the orbit. A circular orbit has an eccentricity \(\varepsilon\) of 0 . An elliptical orbit has an eccentricity of \(0 \leq \varepsilon \leq 1\). If \(\varepsilon>1\), the satellite follows a hyperbolic path and escapes from the Earth's gravitational field. Consider a satellite with a size parameter \(p=1000 \mathrm{~km}\). Plot the orbit of this satellite if \((a) e=0 ;(b) \varepsilon=0.25 ;(c) \varepsilon=0.5\). How close does each orbit come to the Earth? How far away does each orbit get from the Earth? Compare the three plots you created. Can you determine what the parameter \(p\) means from looking at the plots?

See all solutions

Recommended explanations on Psychology Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free