[Tutor] Binary Addition
bob gailer
bgailer at gmail.com
Wed Feb 13 21:03:56 CET 2013
On 2/13/2013 2:13 PM, Ghadir Ghasemi wrote:
> Hi guys can you tell me what is wrong with this code?. 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)
You have some incomplete try: statements. try must be followed by
except: and/or finally:
You should limit the scope of try: to just the one or few statements
that you expect to raise the exception. And you should have an except
clause for that exception.
There is no need to define eight_digit_binary and eight_digit_binary2,
as they do the same thing.
There is no need to nest these defs inside the loop.
You have a lot of other unnecessarily duplicated code.
There is no need to say while on == True:
It is sufficient to say while on:
Your program will never terminate as you never change the value of on.
--
Bob Gailer
919-636-4239
Chapel Hill NC
More information about the Tutor
mailing list