Write a program that asks a user for a weight in pounds (possibly fractional), and outputs the weight in kilograms. (For the record, 1 pound equals approximately 0.45359 kilograms.)
lbs = -1
while lbs < 0:
try:
lbs = float(input('Enter a weight in pounds: '))
if lbs < 0:
print('Weights must be nonnegative.')
except ValueError:
print('That is not a valid number.')
kg = 0.45359 * lbs
print('That is equal to', kg, 'kilograms.')
Assume that we are writing a function
Give an implementation of such a function that rigorously checks that the parameters adhere to these expectations. (Once you've performed the parameter-checking, you don't need to do anything within the function body.)
def doSomething(word, k):
if not isinstance(word, str):
raise TypeError('word must be a string')
if len(word) == 0:
raise ValueError('word must be nonempty')
if not isinstance(k, int):
raise TypeError('k must be an integer')
if k < 0:
raise ValueError('k must be nonnegative')
if k > len(word):
raise ValueError('k must be no greater than the length of the word')