[Tutor] Binary Addition

Alan Gauld alan.gauld at btinternet.com
Wed Feb 13 20:50:48 CET 2013


On 13/02/13 19:13, Ghadir Ghasemi wrote:
> Hi guys can you tell me what is wrong with this code?.

Quite a few things.
Your try lines don't have matching excepts....
You have a function definitions in the middle that have
indentation errors.

eg.
       def eight_digit_binary1(message):
           response1 = input(message)
       while len(response1) > 8:
           response1 = input(message)
       return (response1)

should be:

       def eight_digit_binary1(message):
           response1 = input(message)
           while len(response1) > 8:
               response1 = input(message)
           return (response1)

Except that its buggy (what is returned if the user just hits Enter? Or 
types "fabulous"), but at least the indentation is right!

Also, since you never seem to change 'on' anywhere you might as well use 
a while True loop....

It says unexpected indent and the bottom of the code everytime I run it. 
  Thank you
>
>
> on=True
> while on == True:
>      def eight_digit_binary1(message):
>          response1 = input(message)
>      while len(response1) > 8:
>          response1 = input(message)
>      return (response1)
>
>
>      try:
>
>          binary1 = eight_digit_binary1('please enter the first 8 bit binary number: ');
>          denary1 = 0
>          place_value1 = 1
>
>          for i in binary1 [::-1]:
>                  denary1 += place_value1 * int(i)
>                  place_value1 *= 2
>
>
>          def eight_digit_binary2(message):
>              response2 = input(message)
>          while len(response2) > 8:
>              response2 = input(message)
>          return (response2)
>
>
>          try:
>
>              binary2 = eight_digit_binary2('please enter the first 8 bit binary number: ');
>              denary2 = 0
>              place_value2 = 1
>
>              for i in binary2 [::-1]:
>                      denary2 += place_value2 * int(i)
>                      place_value2 *= 2
>
>              def print_result(result):
>                      result = (place_value1 + place_value2)
>                      remainder = ''
>              while result > 0:
>                      remainder = str(result % 2) + remainder
>                      result >>= 1
>              print("The result is",remainder)
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list