Читать книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer - Страница 69
End-of-Chapter Exercises
Оглавление2.1 Write Python code that uses the input built-in function to ask the user to enter a whole number between 1 and 100. The input function always returns a string value, so use the int built-in function to convert the value entered to an integer data type and square the number that the user entered using the exponentiation operator. Print a message to the user stating the value that they entered and the square of the value that they entered.
2.2 Write Python code that uses the input built-in function to ask the user to enter the year they were born as a four-digit number. The input function always returns a string value, so use the int built-in function to convert the year value entered to an integer data type and subtract the year entered from the current year (i.e., 2019). Print a message to the user stating the value that they entered and their calculated age. Why might the calculated age not be correct?
2.3 Write Python code that uses the input built-in function to ask the user to enter a decimal formatted number between 1 and 100. The input function always returns a string value, so use the float built-in function to convert the value entered to a float data type and square the number that the user entered using the exponentiation operator. Print a message to the user stating the value that they entered and the square of the value that they entered.
2.4 Modify the code in Exercise 2.3 to round the values reported to the user to two decimal places (use the round built-in function).
2.5 Write Python code that uses the input built-in function to ask the user to enter a sentence of their choosing. Use the len built-in function to determine how many characters were in the string entered and report this information back to the user.
2.6 Write Python code that uses the input built-in function to ask the user to enter a weight in pounds. The input function always returns a string value, so use the float built-in function to convert the value entered to a float data type and determine the equivalent weight in kilograms (you can use the conversion factor that 1 pound = 0.453592 kilograms). Print a message to the user stating the weight in pounds that they entered and the equivalent weight in kilograms.
2.7 Write Python code that uses the input built-in function to ask the user to enter a temperature in the Fahrenheit temperature scale. The input function always returns a string value, so use the float built-in function to convert the value entered to a float data type and determine the equivalent temperature in the Celsius temperature scale (use the conversion factor °C = (°F – 32) × (5/9)). Print a message to the user stating the temperature in Fahrenheit that they entered and the equivalent temperature in Celsius. You can verify that your code executes properly by entering in 32°F (equivalent is 0°C) and 212°F (equivalent is 100°C).