Python Programming Practice Test 2
8 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Write a Python solution that accepts three integer inputs representing the number of times an employee travels to a job site. Output the total distance traveled to two decimal places given the following miles per employee commute to the job site: Employee A: 15.62 miles Employee B: 41.85 miles Employee C: 32.67 miles The solution output should be in the format Distance: total_miles_traveled miles

travels = {"A": int(input()), "B": int(input()), "C": int(input())} miles_per_employee = {"A": 15.62, "B":41.85, "C": 32.67} total_miles_traveled = sum(travels[employee] * miles_per_employee[employee] for employee in travels) print(f"Distance: {total_miles_traveled:.2f} miles")

Write a Python solution that accepts an integer input representing any number of ounces. Output the converted total number of tons, pounds, and remaining ounces based on the input ounces value. There are 16 ounces in a pound and 2,000 pounds in a ton. The solution output should be in the format Tons: value_1 Pounds: value_2 Ounces: value_3

ounces = int(input()) value_1 = ounces // (16 * 2000) value_2 = (ounces % (16 * 2000)) // 16 value_3 = ounces % 16 print(f"Tons: {value_1}") print(f"Pounds: {value_2}") print(f"Ounces: {value_3}")

Write a Python solution that accepts an integer input representing the index value for any any of the five elements in the following list: various_data_types = [516, 112.49, True, "meow", ("Western", "Governors", "University"), {"apple": 1, "pear": 5}] Using the built-in function type() and getting its name by using the .name attribute, output data type (e.g., int", "float", "bool", "str") based on the input index value of the list element. The solution output should be in the format Element index_value: data_type

index_value = int(input()) data_type = type(various_data_types[index_value]).name print(f"Element {index_value}: {data_type}")

Write a Python solution that accepts any three integer inputs representing the base (b1, b2) and height (h) measurements of a trapezoid in meters. Output the exact area of the trapezoid in square meters as a float value. The exact area of a trapezoid can be calculated by finding the average of the two base measurements, then multiplying by the height measurement. Trapezoid Area Formula: $A = [(b1 + b2) / 2] * h$ The solution output should be in the format Trapezoid area: area_value square meters

<p>b1 = int(input()) b2 = int(input()) h = int(input()) area_value = float((b1 + b2) /2) * h print(f&quot;Trapezoid area: {area_value} square meters&quot;)</p> Signup and view all the answers

Write a Python solution that accepts five integer inputs. Output the sum of the five inputs three times, converting the inputs to the requested data type prior to finding the sum. First output: sum of five inputs maintained as integer values Second output: sum of five inputs converted to float values Third output: sum of five inputs converted to string values (concatenate) The solution output should be in the format Integer: integer_sum_value Float: float_sum_value String: string_sum_value

<p>num1 = int(input()) num2 = int(input()) num3 = int(input()) num4 = int(input()) num5 = int(input()) integer_sum_value = num1 + num2 + num3 + num4 + num5 float_sum_value = float(num1) + float(num2) + float(num3) + float(num4) + float(num5) string_sum_value = str(num1) + str(num2) + str(num3) + str(num4) + str(num5) print(f&quot;Integer: {integer_sum_value}&quot;) print(f&quot;Float: {float_sum_value}&quot;) print(f&quot;String: {string_sum_value}&quot;)</p> Signup and view all the answers

Write a Python solution that accepts an integer input representing a 9-digit unformatted student identification number. Output the identification number as a string with no spaces. The solution output should be in the format 111-22-3333

<p>student_id = int(input()) student_id &gt;= 100000000 and student_id max_value: <code>boolean_value = True</code> print(f&quot;Greater Than Max? {boolean_value}&quot;) else: print(f&quot;Greater Than Max? {boolean_value}&quot;)</p> Signup and view all the answers

Write a Python solution that accepts one integer input representing the index value for any of the string elements in the following list: frameworks = ["Django", "Flask", "CherryPy", "Bottle", "Web2Py", "TurboGears"] Output the string element of the index value entered. The solution should be placed in a try block and implement an exception of "Error" if an incompatible integer input is provided. The solution output should be in the format frameworks_element

<p>try: index = int(input()) frameworks_element = (frameworks[index]) print(frameworks_element)</p> <p>except (ValueError, IndexError): print(&quot;Error&quot;)</p> Signup and view all the answers

Write a Python solution that accepts an integer input representing water temperature in degrees Fahrenheit. Output a description of the water state based on the following scale: If the temperature is below 33° F, the water is "Frozen". If the water is between 33° F and 80° F (including 33), the water is "Cold". If the water is between 80° F and 115° F (including 80), the water is "Warm". If the water is between 115° F and 211° (including 115) F, the water is "Hot". If the water is greater than or equal to 212° F, the water is "Boiling". Additionally, output a safety comment only during the following circumstances: If the water is exactly 212° F, the safety comment is "Caution: Hot!" If the water temperature is less than 33° F, the safety comment is "Watch out for ice!" The solution output should be in the format water_state optional_safety_comment

<p>temperature = int(input()) water_state = &quot;&quot; optional_safety_comment = &quot;&quot; if temperature &lt; 33: water_state = &quot;Frozen&quot; optional_safety_comment = &quot;Watch out for ice!&quot; elif 33</p> Signup and view all the answers

Flashcards

Total Distance Calculation

Calculate total miles from employee travel inputs multiplied by their commute distances.

Distance Traveled Code

Uses inputs for number of trips to compute total travel distance.

Ounces to Tons Conversion

Convert ounces input to tons, pounds, and remainder ounces.

Weight Conversion Code

Accepts ounces as input to compute its equivalent in tons, pounds, and ounces.

Signup and view all the flashcards

Data Type by Index

Retrieves and outputs the data type of a list element based on index input.

Signup and view all the flashcards

Data Type Identification Code

Uses input index to find data type of an element in a list.

Signup and view all the flashcards

Trapezoid Area Formula

Calculates trapezoid area using average of bases and height.

Signup and view all the flashcards

Trapezoid Area Calculation

Takes base measurements and height to compute trapezoid area.

Signup and view all the flashcards

Sum of Five Integers

Calculates and shows sum results in integer, float, and string types.

Signup and view all the flashcards

Type Conversion Output

Outputs sums of inputs in integer, float, and string formats.

Signup and view all the flashcards

Student ID Formatting

Outputs a student ID formatted as a string without spaces.

Signup and view all the flashcards

Safe ID Checks

Ensures the ID is a 9-digit number before processing.

Signup and view all the flashcards

Web Frameworks List

Outputs an element from a list of web frameworks based on index input.

Signup and view all the flashcards

Framework Index Code

Retrieves a framework name based on input index; handles errors.

Signup and view all the flashcards

Water State Determination

Describes the state of water based on Fahrenheit temperature input.

Signup and view all the flashcards

Temperature Conditionals

Uses temperature to classify water state and give safety comments.

Signup and view all the flashcards

Safety Comments for Water

Outputs safety comments under certain temperature conditions.

Signup and view all the flashcards

Input Collection for Calculations

Collects multiple integer inputs for processing.

Signup and view all the flashcards

List Element Types

Identifies data types based on indexed elements in a list.

Signup and view all the flashcards

Average in Geometry

Calculates average as part of trapezoid area formula.

Signup and view all the flashcards

Modulus Operator

Used to find the remainder in conversions.

Signup and view all the flashcards

Error Handling in Python

Utilizes try-except structure for managing runtime errors.

Signup and view all the flashcards

Function Printing

Printing formatted strings to display calculated results.

Signup and view all the flashcards

Floating Point Arithmetic

Deals with decimal points in calculations creatively.

Signup and view all the flashcards

Concatenation in Strings

Combines string representations of numbers in output.

Signup and view all the flashcards

Integer Division

Finds the quotient without remainder when dividing integers.

Signup and view all the flashcards

Conversion Between Types

Shows how to easily change data types in Python.

Signup and view all the flashcards

Summation Logic

Logic structure to sum multiple inputs intelligently.

Signup and view all the flashcards

Logical Control Flow

Uses conditionals to control program flow based on inputs.

Signup and view all the flashcards

Study Notes

Python Programming Practice Test 2 - Study Notes

  • Employee Travel Distance Calculation:

    • Collects integer inputs for employee travel frequencies ('A', 'B', 'C').
    • Uses a dictionary for employee travel costs {'A': 15.62, 'B': 41.85, 'C': 32.67}.
    • Calculates total distance using a generator expression.
    • Prints the total distance to two decimal places in the format "Distance: total_miles_traveled miles".
  • Ounces to Tons, Pounds, and Ounces Conversion:

    • Takes integer input for ounces.
    • Calculates tons, pounds, and remaining ounces using integer division and modulo.
    • Prints the results in the format "Tons: value_1 Pounds: value_2 Ounces: value_3".
  • Determining Data Type by Index:

    • Receives an integer input representing an index in a predefined list ("various_data_types").
    • Uses type() to get the data type and its name (__name__).
    • Prints the result in the format "Element index_value: data_type".
  • Trapezoid Area Calculation:

    • Takes integer inputs for base 1 (b1), base 2 (b2), and height (h) in meters.
    • Calculates the area using the formula: area = ((b1 + b2) / 2) * h.
    • Prints the area as a float in the format "Trapezoid area: area_value square meters".
  • Sum of Five Inputs (Different Types):

    • Takes five integer inputs.
    • Calculates the sum as an integer, float, and string concatenation.
    • Prints the results in the format "Integer: integer_sum_value Float: float_sum_value String: string_sum_value".
  • Outputting a 9-digit Student ID:

    • Accepts a 9-digit integer student ID.
    • Outputs the ID as a string without spaces in the format required: "111-22-3333" (unclear how formatting works; text only shows formatting).
  • Handling List Element Retrieval by Index (Error Handling):

    • Takes an integer input for an index in the list "frameworks".
    • Uses a try-except block to handle potential ValueError (user input error) and IndexError (index out of range).
    • Prints the element or "Error" if an error occurred.
  • Water Temperature State and Safety Comment:

    • Takes an integer water temperature input in degrees Fahrenheit.
    • Determines water state ("Frozen", "Cold", "Warm", "Hot", "Boiling") based on temperature ranges.
    • Prints the state and safety comment (extra comment for temperature limits or safety situations).

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Description

Test your knowledge of Python programming with this interactive quiz. It covers topics such as employee travel distance calculation, unit conversions, and data type identification. Ideal for students looking to reinforce their programming skills.

Use Quizgecko on...
Browser
Browser