When do I need to convert strings to numbers or vice versa?Show Answer
In general, because the input function
always returns a string, if you want the program's
user to type in numbers, you'll need to convert from strings to
numbers whenever you use input. Similarly, because
print often deals with strings as messages, whenever you
want to use concatenation to include numbers in those messages,
you'll need to covert from a number to a string first. Note that in
some cases you can avoid this by passing multiple arguments to
print.
Why should we go to the trouble of establishing expectations for our code?Show Answer
There are two good answers to this question:
Expectations are part of the problem-solving
process: By establishing expectations
before you write your code, you will test your
understanding of the problem you're trying to solve. The most
costly errors in terms of time wasted are errors in understanding
a problem, where you accidentally solve the wrong problem and
have to go back and redo a lot of work. Being clear about your
expectations up front forces you to state what a solution will
look like and thus reduces the possibility of misunderstanding
the problem.
Expectations help avoid errors: they allow you
to set up tests that make sure your code is working, and these
tests will happen every time your code is run.
It is extremely easy to make a small change that accidentally
breaks something, including things that had been working before
which you already tested. By running tests automatically, and
running all tests each time you run your code, you'll be able to
quickly see what is broken and therefore fix it more easily.