[Tutor] What is wrong with my code?

Dave Angel davea at davea.name
Mon Mar 30 03:59:08 CEST 2015


On 01/23/2015 04:40 PM, Antonia van der Leeuw wrote:
> Hehey!
>
> I'm learning python on a website called codecademy.com, where I made a
> program to decode binary numbers. I guess the site uses a different
> compiler, because on the site my code worked fine, but when I copied and
> pasted it into the Python IDLE (3.4.2) it didn't work!

When asking a question here, it's really more useful to say in what way 
it didn't work.  Like if you crashed with an exception, show the stack 
trace including the error.

Still, it's a pretty safe guess that you got an exception on the print 
statement(s), which is a function in Python 3.x.

  I'm really don't know
> what is wrong with my code, can anyone of you fine sirs help me?
>
>
>
> Meh code:
>
>
>
> number_input = input("What binary number do you want me to decode? ")
>
>
>
> def decoder(number):
>
>      number_backwards = str(number)[::-1] # Because binary numbers go from
> right to left.
>
>      result = 0
>
>      value = 1
>
>      br = False
>
>      for n in number_backwards:
>
>          if n != "1" and n != "0":
>
>              print number, "is not a binary number"
>

          print(number, "is not a binary number")

>              br = True
>
>              break
>
>          elif n == "1":
>
>              result += value
>
>          value += value
>
>      if br == False:
>
>          print "The binary number decoded is", result
>

       print("The binary number decoded is", result)
>
>
> decoder(number_input)
>
>-- 
DaveA


More information about the Tutor mailing list