# [Tutor] (no subject)

Joel Goldstick joel.goldstick at gmail.com
Sat Jan 12 19:00:26 CET 2013

```First, you need to type in a useful subject line for your question.
Second, it is nicer if you use plain text instead of rtf because rtf messes
up indentations sometimes.

This looks like homework.  That's ok.  But you should say so.  You haven't
really asked a question.  You state the assignment, then say that you don't
know how to deal with the 8 digit limit.

The int() function will raise a value error if you give it a number that
doesn't work for the base you set:

>>> int('123',2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 2: '123'
>>>

So, you could write a small function that reads in the user's data and then
checks its length and whether it doesn't raise the V alueError
If it passes both tests, return its value.  Call it again for the second
number. Since you ask a different prompt for each number, you should pass
the prompt into your function as a parameter.  Add the two values, convert
to binary, and display your result

On Sat, Jan 12, 2013 at 12:32 PM, Ali Raza Ghasemi <
ghasema01 at leedslearning.net> wrote:

>  I have to make a program that adds binary numbers together. The program
> has to accept two binary values (up to 8 binary digits) and output their
> total in binary. The output should not contain any leading zeros.
> I have a problem in that I don't know how to limit the number digits to 8
> as it accepts however many digits you enter, I also want the  program to
> ignore anything other than 8-bit 0s and 1s. It is a python version 3.2.3
> Here is my code:
>
>     while True:
>         return num1 + num2
>         if len(str(num1)) > 8:
>             print("Please enter an 8 bit binary number")
>             continue
> num1 = int(input('please enter the first 8 bit binary number: '),2)
> num2 = int(input('please enter the second 8 bit binary number: '),2)
> print('the result is', bin(result)[2:])
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

--
Joel Goldstick
http://joelgoldstick.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130112/37d60391/attachment-0001.html>
```