In this review I'll focus on a few habits you should cultivate if you want to
become proficient in programming.
Learn the Python naming conventions. For example, lowercase for variable
and function names, UPPERCASE for constants, and CamelCase for classes.
Use functions for all of your code. At the top level of your script, there
should be nothing happening other than imports, constants, and function (or
class) definitions. Adopt that discipline from the outset and your learning
will progress faster.
Embrace command-line arguments. The most successful and ubiquitous
scripts/commands in computing history (think of the typical commands in a Unix
shell, or their analogues in Windows) do not engage in tedious dialogues with
their users. Instead, they take their inputs directly from command line
arguments, do something, and then exit -- that's it. That model has been so
successful because it allows different commands to be combined in various ways
(for example, the output of one command piped directly as input into another)
and for the commands to operate successfully without any human intervention at
all. You may not appreciate all of those benefits yet, but if you continue with
programming, your appreciation will grow. The way to start is to design your
programs so that they run primarily from command-line arguments (sys.argv
in
the example below). I left input()
in the program simply to be faithful to
your original code, but in my own programs I (almost) never use it.
If you must use input()
don't do it more than necessary. Python can
easily parse inputs like "75 kg". No need to extend the computer dialogue
into separate questions.
Build programs by combining the behaviors of small, well-focused functions.
In the example below, we have a function to get the weight from the command-line
arguments (or from user input) and another function to convert the weight.
Use data structures to simplify logic and improve code readability.
In the example, the conversion is driven entirely by data that is easy
to understand.
Keep messages to users brief and to-the-point. If you can rewrite a message
in a more compact way, do it. Less verbose, less chatty messages are easier to
maintain in your code and they are less of a cognitive burden on users.
Next steps: add validation to the input handling. If the user provides
invalid inputs, get_weight()
or convert()
will fail. But you can easily fix that. There
are multiple examples on CodeReview (I have written a couple) illustrating how
to build a general-purpose function to get user input, convert it, and validate
it.
import sys
CONVERSIONS = {
'lb': (0.453592, 'kg'),
'kg': (2.205, 'lb'),
}
def main(args):
weight, unit = get_weight(args)
converted, new_unit = convert(weight, unit)
print(f'That equals {converted} {new_unit}.')
def get_weight(args):
if args:
weight, unit = args
else:
prompt = 'Enter your weight and its units (lb or kg): '
reply = input(prompt)
weight, unit = reply.strip().lower().split()
return (float(weight), unit)
def convert(weight, unit):
factor, new_unit = CONVERSIONS[unit]
return (round(weight * factor, 2), new_unit)
if __name__ == '__main__':
main(sys.argv[1:])