WGU D335 Practice Test 2 10 studiers today 5.0 (1 review) Students also studied Terms in this set (15) Western Governors UniversityD 333 Save C949 WGU Terminology 72 terms VeraButlerPreview WGU C949 Data Structures and Alg...102 terms bodiewoodPreview
WGU D 335: Introduction to Progra...
15 terms xxDragonflamezxx Preview Intro to Teacher cor 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
times_traveledA = int(input()) times_traveledB = int(input()) times_traveledC = int(input()) employeeA = 15.62 #miles employeeB = 41.85 #miles employeeC = 32.67 #miles distance_traveledA = times_traveledA * employeeA distance_traveledB = times_traveledB * employeeB distance_traveledC = times_traveledC * employeeC total_miles_traveled = distance_traveledA + distance_traveledB + distance_traveledC print('Distance: {:.2f} miles'.format(total_miles_traveled)) 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.file_name = input()
with open(file_name, 'r') as f:
word1 = str(f.readline()).strip() word2 = str(f.readline()).strip() word3 = str(f.readline()).strip() f = open(file_name, 'r') lines = f.read().splitlines() lines = ' '.join(lines) f.close() print(f'{word1}\n{word2}\n{word3}\n{lines}')
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.ounces_per_pound = 16 pounds_per_ton = 2000 number_ounces = int(input()) tons = number_ounces // (ounces_per_pound * pounds_per_ton) remaining_ounces = number_ounces % (ounces_per_pound * pounds_per_ton) pounds = remaining_ounces // ounces_per_pound remaining_ounces = remaining_ounces % ounces_per_pound
print('Tons: {}'.format(tons))
print('Pounds: {}'.format(pounds))
print('Ounces: {}'.format(remaining_ounces))
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.import csv input1 = input()
with open(input1, "r") as f:
data = [row for row in 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) 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.index_value = int(input()) name = various_data_types[index_value] data_type = type(name).__name__
print(f"Element {index_value}: {data_type}")
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.import math fact = int(input()) x = math.factorial(fact) print(x)
if x > 100:
print('True')
else:
print('False') 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
b1 = int(input()) b2 = int(input()) h = int(input()) area_value = ((b1 + b2) /2) * h print('Trapezoid area: {:.1f} square meters'.format(area_value))
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.import pigAge
def pigAge_converter(pig_age):
return pig_age * 5 pig_age = int(input()) converted_pig_age = pigAge_converter(pig_age) print(f'{pig_age} is {converted_pig_age} in human years') 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) num1 = int(input()) num2 = int(input()) num3 = int(input()) num4 = int(input()) num5 = int(input()) first_output = num1 + num2 + num3 + num4 + num5 second_output = float(num1) + float(num2) + float(num3) + float(num4) + float(num5) third_output = str(num1) + str(num2) + str(num3) + str(num4) + str(num5)
print('Integer: {}'.format(first_output))
print('Float: {}'.format(second_output))
print('String: {}'.format(third_output))
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.store_item = input() num_items = int(input()) total_cost = purchase[store_item] * num_items
if num_items < 10:
print(store_item, "${:.2f}".format(total_cost))
if num_items in range(10,21):
discount = total_cost * 0.05 total_cost = total_cost - discount
print(store_item, '${:.2f}'.format(total_cost))
if num_items >= 21:
discount = total_cost * 0.10 total_cost = total_cost - discount
print(store_item, '${:.2f}'.format(total_cost))
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.student_id = int(input()) student_id_string = str(student_id)
first3 = student_id_string[0:3]
second2 = student_id_string[3:5]
third4 = student_id_string[5:]
print(f'{first3}-{second2}-{third4}')
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.num_stock = int(input()) total_cost = 0
for i in range(num_stock):
stock_selection = input()
if stock_selection in stocks:
total_cost += stocks[stock_selection] print('Total price: ${:.2f}'.format(total_cost)) 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 num = int(input()) boolean_value = False max_value = max(predef_list)
if num > max_value:
boolean_value = True print('Greater Than Max? {}'.format(boolean_value))
else:
print('Greater Than Max? {}'.format(boolean_value)) 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!" temperature = int(input())
if temperature <33:
print('Frozen') print('Watch out for ice!')
if temperature in range(33, 80):
print('Cold')
if temperature in range(80, 115):
print('Warm')
if temperature in range(115, 211):
print('Hot')
if temperature >= 212:
print('Boiling')