WGU D335 Practice Test 2
Study online at https://quizlet.com/_eziszg
1. Create a solution that accepts three integer times_traveledA = int(input())
inputs representing the number of times an times_traveledB = int(input())
employee travels to a job site. Output the times_traveledC = int(input())
total distance traveled to two decimal places employeeA = 15.62 #miles
given the following miles per employee com- employeeB = 41.85 #miles
mute to the job site. Output the total distance employeeC = 32.67 #miles
traveled to two decimal places given the fol- distance_traveledA = times_traveledA *
lowing miles per employee commute to the employeeA
job site: distance_traveledB = times_traveledB *
Employee A: 15.62 miles employeeB
Employee B: 41.85 miles distance_traveledC = times_traveledC *
Employee C: 32.67 miles employeeC
total_miles_traveled = distance_travele-
dA + distance_traveledB + distance_trav-
eledC
print('Distance: {:.2f} miles'.format(to-
tal_miles_traveled))
2. Create a solution that accepts an input iden- file_name = input()
tifying the name of a text file, for example, with open(file_name, 'r') as f:
"WordTextFile1.txt". Each text file contains word1 = str(f.readline()).strip()
three rows with one word per row. Using the word2 = str(f.readline()).strip()
open() function and write() and read() meth- word3 = str(f.readline()).strip()
ods, interact with the input text file to write a
new sentence string composed of the three f = open(file_name, 'r')
existing words to the end of the file contents lines = f.read().splitlines()
on a new line. Output the new file contents. lines = ' '.join(lines)
f.close()
print(f'{word1}\n{word2}\n{word3}\n{lines}
3. Create a solution that accepts an integer ounces_per_pound = 16
input representing any number of ounces. pounds_per_ton = 2000
1/7
, WGU D335 Practice Test 2
WGU D335 Practice Test 2
Study online at https://quizlet.com/_eziszg
Output the converted total number of tons, number_ounces = int(input())
pounds, and remaining ounces based on the tons = number_ounces
input ounces value. There are 16 ounces in a // (ounces_per_pound *
pound and 2,000 pounds in a ton. pounds_per_ton)
remaining_ounces = num-
ber_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(remain-
ing_ounces))
4. Create a solution that accepts an input iden- import csv
tifying the name of a CSV file, for example, input1 = input()
"input1.csv". Each file contains two rows of with open(input1, "r") as f:
comma-separated values. Import the built-in data = [row for row in csv.reader(f)]
module csv and use its open() function and for row in data:
reader() method to create a dictionary of even = [row[i].strip() for i in range(0,
key:value pairs for each row of comma-sep- len(row), 2)]
arated values in the specified file. Output the odd = [row[i].strip() for i in range(1,
file contents as two dictionaries. len(row), 2)]
pair = dict(zip(even, odd))
print(pair)
5. Create a solution that accepts an integer in- index_value = int(input())
put representing the index value for any any name = various_data_types[index_val-
of the five elements in the following list: ue]
various_data_types = [516, 112.49, True, data_type = type(name).__name__
2/7