> I am trying to make a binary counter that will prompt for and read a decimal 
> number (whole number). Then display all decimal numbers starting from 1 up to 
> and including the decimal number entered along with the binary representation 
> of the numbers to the screen.

You might consider writing a separate function toBinary() that takes
as input a number 'n' and returns its binary representation as a
string.  You're copying and pasting, and that's a sign that you've got
a block of code that you can *reuse*.  If you don't know how to write
functions, please ask!


Also, you can write a loop that goes from 1 to N by using range().  For example:

########################
for n in range(1, N+1):
    print(n, 2*n)
########################

The while loop that you have does work, but the for loop here is more
idiomatic in expressing the idea of "Do the following for this
collection of values ..."

... Reading the code...


Ah.  You have a fixed number of variables to capture values such as
next_num_1, binary_num_1, next_num_2, binary_num_2, and so on.  But
this means you'll only be able to handle a fixed number of values,
where by "fixed", it looks like you've gone up to four.  :P

As you're noticing, this approach with capturing results with a fixed
number of variables isn't going to work well when we don't know how
many times we're walking through the loop.


Do you know about lists?  They allow you to hold onto a variable-sized
collection of values.  For example, let's say that we want to produce
the output:

###########
1 2
2 4
3 6
4 8
5 10
...
###########

Basically, our 2-times table.


Here's how we can do this.

#########################
## pseudocode
inputs = []
outputs = []
for x in range(10):
    inputs.append(x)
    outputs.append(x * 2)
#########################


Then we can get at any particular input/output by indexing the list at
the same position.  For example, we can print the inputs and outputs
like this:

#############################
inputs = []
outputs = []
for x in range(10):
    inputs.append(x)
    outputs.append(x * 2)
for (i, o) in zip(inputs, outputs):
   print (i,o)
#############################

and this takes an approach similar to what you've got, but it works
because it can hold onto all the results in a variable-sized list.



But that being said, for your particular program, you might not even
need to hold onto the entire collection of inputs and outputs at once.
Can you just do something like this instead?

###########################################
for x in range(10):
    doubled = x * 2
    print(x, doubled)
###########################################

where we interleave computation with output within the loop itself?
This has the advantage that we don't need to hold onto anything but
the very last thing we just computed, so it reduces the number of
things we're juggling to just the range that we're walking over, the
current value that we're walking, and the output from that current
value.
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to