Homework 2
Part 1 - Weight Converter
def weightConverter():
planets = []
# Welcome the user
print("Hello!")
# Collect planets and multipliers in a list of [name, multiplier]
user_input = input("Please type in a pair of (planet,multiplier), or hit enter to continue: ")
while user_input:
planet, weight = user_input.split(",")
planets.append([planet, float(weight)])
user_input = input("Please type in a pair of (planet,multiplier), or hit enter to continue: ")
# Now ask the user for their weight and convert to a float
user_weight = float(input("Please tell me your weight on Earth: "))
# Ask for targets to convert
user_input = input("Where do you want to know your weight? (or hit enter to exit) ")
while user_input:
# Fine the planet in the planets list
for planet, weight in planets:
# If it matches...
if planet == user_input:
# ...print out the weight
print(f"Your weight on {planet} is: {weight * user_weight}")
# and ask again
user_input = input("Where do you want to know your weight? (or hit enter to exit) ")
# Print goodbye and exit
print("Bye!")
weightConverter()
Part 2 - Anonymizer
from string import punctuation
def textAnonymizer(input_text, words_to_remove):
# Convert input list to space-separated list
input_text_as_list = input_text.split(" ")
# Walk through input text
for i in range(len(input_text_as_list)):
# Grab word
input_word = input_text_as_list[i]
# NOTE: you can hardcode here, don't need to go overboard
cleaned_up_input_word = input_word.strip(punctuation)
for word_to_remove in words_to_remove:
if cleaned_up_input_word == word_to_remove:
# Replace in input text
input_word = input_word.replace(word_to_remove, "*"*len(word_to_remove))
# And replace in original list
input_text_as_list[i] = input_word
# Finally, reassemble the input text
anonymized_text = " ".join(input_text_as_list)
return anonymized_text
def runTextAnonymizer():
# Ask user for input text and convert to space-separated list
input_text = input("Please type in a block of text from which to prune sensitive information: ")
# Ask user for comma-separated words and convert to list
words_to_remove = input("Please type in a comma-separated list of words to remove: ")
words_to_remove_as_list = words_to_remove.split(",")
# Run the text anonymizer
anonymized_text = textAnonymizer(input_text, words_to_remove_as_list)
return anonymized_text
# Text cases
# print(textAnonymizer("This is a test", ["test"]))
# print(textAnonymizer("This is a test", ["is"]))
# print(textAnonymizer("Lorem ipsum dolor sit amet, consectdolor asitamet samet", ["dolor", "sit", "amet"]))
# Interactive run
print(runTextAnonymizer())