[Tutor] Binary Addition
Mark Lawrence
breamoreboy at yahoo.co.uk
Wed Feb 13 20:51:08 CET 2013
On 13/02/2013 19:13, 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)
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
The while loops and return statements in the eight_digit_binary1/2
functions aren't correctly indented. Why put these functions within the
outer while loop? The trys aren't matched up with anything, you need
something else, usually one or more except clauses.
--
Cheers.
Mark Lawrence
More information about the Tutor
mailing list