D335 / D 335 Test Sections 2 (Latest Update
) Introduction to Programming in
Python | Questions and Answers | Grade A |
100% Correct. WGU
1. **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.**
ANS 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")
, 2
2. **Create a solution that accepts an input identifying the name of a text file,
for example, "input.txt". The file contains three words separated by spaces on
a single line. Output the three words as a single period-separated sentence.**
ANS 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)