WGU D 335: Introduction to Programming in Python home: Practice Test 2

0.0(0)
studied byStudied by 1 person
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/14

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

15 Terms

1
New cards

Create a 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. 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")

2
New cards

Create a 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}")

3
New cards

Create a 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}")

4
New cards

Create a 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

b1 = int(input())

b2 = int(input())

h = int(input())

area_value = float((b1 + b2) /2) * h

print(f"Trapezoid area: {area_value} square meters")

5
New cards

Create a 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

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"Integer: {integer_sum_value}")

print(f"Float: {float_sum_value}")

print(f"String: {string_sum_value}")

6
New cards

Create a 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

student_id = int(input())

student_id >= 100000000 and student_id <= 999999999

part1 = student_id // 1000000

part2 = (student_id // 10000) % 100

part3 = student_id % 10000

formatted_id = f"{part1}-{part2}-{part3}"

print(formatted_id)

7
New cards

Create a solution that accepts an integer input to compare against the following list:

predef_list = [4, -27, 15, 33, -10]

Output a Boolean value indicating whether the input value is greater than the maximum value from predef_list

The solution output should be in the format

Greater Than Max? Boolean_value

num = int(input())

boolean_value = False

max_value = max(predef_list)

if num > max_value:

boolean_value = True

print(f"Greater Than Max? {boolean_value}")

else:

print(f"Greater Than Max? {boolean_value}")

8
New cards

Create a 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

try:

index = int(input())

frameworks_element = (frameworks[index])

print(frameworks_element)

except (ValueError, IndexError):

print("Error")

9
New cards

Create a 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

temperature = int(input())

water_state = ""

optional_safety_comment = ""

if temperature < 33:

water_state = "Frozen"

optional_safety_comment = "Watch out for ice!"

elif 33 <= temperature < 80:

water_state = "Cold"

elif 80 <= temperature < 115:

water_state = "Warm"

elif 115 <= temperature < 212:

water_state = "Hot"

elif temperature >= 212:

water_state = "Boiling"

if temperature == 212:

optional_safety_comment = "Caution: Hot!"

print(water_state)

if optional_safety_comment:

print(optional_safety_comment)

10
New cards

Create a solution that accepts an integer input identifying how many shares of stock are to be purchased from the Old Town Stock Exchange, followed by an equivalent number of string inputs representing the stock selections. The following dictionary stock lists available stock selections as the key with the cost per selection as the value.

stocks = {'TSLA': 912.86 , 'BBBY': 24.84, 'AAPL': 174.26, 'SOFI': 6.92, 'KIRK': 8.72, 'AURA': 22.12, 'AMZN': 141.28, 'EMBK': 12.29, 'LVLU': 2.33}

Output the total cost of the purchased shares of stock to two decimal places.

The solution output should be in the format

Total price: $cost_of_stocks

num_stock = int(input())

cost_of_stocks = 0

for _ in range(num_stock):

stock_selection = input()

if stock_selection in stocks:

cost_of_stocks += stocks[stock_selection]

print(f"Total price: ${cost_of_stocks:.2f}")

11
New cards

Create a solution that accepts a string input representing a grocery store item and an integer input identifying the number of items purchased on a recent visit. The following dictionary purchase lists available items as the key with the cost per item as the value.

purchase = {"bananas": 1.85, "steak": 19.99, "cookies": 4.52, "celery": 2.81, "milk": 4.34}

Additionally,

If fewer than ten items are purchased, the price is the full cost per item.

If between ten and twenty items (inclusive) are purchased, the purchase gets a 5% discount.

If twenty-one or more items are purchased, the purchase gets a 10% discount.

Output the chosen item and total cost of the purchase to two decimal places.

The solution output should be in the format

item_purchased $total_purchase_cost

item_purchased = input().lower()

num_items = int(input())

if item_purchased in purchase:

total_purchase_cost = purchase[item_purchased]* num_items

if 10 <= num_items <= 20:

total_purchase_cost = total_purchase_cost - (total_purchase_cost*.05)

elif num_items >= 21:

total_purchase_cost = total_purchase_cost - (total_purchase_cost*.10)

print(f"{item_purchased} ${total_purchase_cost:.2f}")

12
New cards

Create a solution that accepts an input identifying the name of a text file, for example, "WordTextFile1.txt". Each text file contains three rows with one word per row. Using the open() function and write() and read() methods, interact with the input text file to write a new sentence string composed of the three existing words to the end of the file contents on a new line. Output the new file contents.

The solution output should be in the format

word1 word2 word3 sentence

file_name = input()

with open(file_name, 'r') as f:

word1 = f.readline().strip()

word2 = f.readline().strip()

word3 = f.readline().strip()

sentence = f"{word1} {word2} {word3}"

with open(file_name, 'a') as f:

f.write(f"\n{sentence}")

with open(file_name, 'r') as f:

lines = f.read().strip()

print(lines)

13
New cards

Create a solution that accepts an input identifying the name of a CSV file, for example, "input1.csv". Each file contains two rows of comma-separated values. Import the built-in module csv and use its open() function and reader() method to create a dictionary of key:value pairs for each row of comma-separated values in the specified file. Output the file contents as two dictionaries.

The solution output should be in the format

{'key': 'value', 'key': 'value', 'key': 'value'} {'key': 'value', 'key': 'value', 'key': 'value'}

import csv

input1 = input()

with open(input1, "r") as f:

data = csv.reader(f)

for row in data:

even = [row[i].strip() for i in range(0, len(row), 2)]

odd = [row[i].strip() for i in range(1, len(row), 2)]

pair = dict(zip(even, odd))

print(pair)

14
New cards

Create a solution that accepts an integer input. Import the built-in module math and use its factorial() method to calculate the factorial of the integer input. Output the value of the factorial, as well as a Boolean value identifying whether the factorial output is greater than 100.

The solution output should be in the format

factorial_value Boolean_value

import math

num= int(input())

factorial_value = math.factorial(num)

print(factorial_value)

if factorial_value > 100:

Boolean_value = True

print(Boolean_value)

else:

Boolean_value = False

print(Boolean_value)

15
New cards

Create a solution that accepts an integer input representing the age of a pig. Import the existing module pigAge and use its pre-built pigAge_converter() function to calculate the human equivalent age of a pig. A year in a pig's life is equivalent to five years in a human's life. Output the human-equivalent age of the pig.

The solution output should be in the format

input_pig_age is converted_pig_age in human years

import pigAge

input_pig_age = int(input())

converted_pig_age = pigAge.pigAge_converter(input_pig_age)

print(f"{input_pig_age} is {converted_pig_age} in human years")