Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python
#2
Sería mejor hacerlo así:

Code:
numero = raw_input("Ingresa codigo binario: ")
numero = numero[::-1] #Ponlo en reverse
resulta = 0
for i, cadaDigito in enumerate(numero):
    if cadaDigito == '1':
        resulta += 1 << i
print(resulta)

I have to look at your code to see what's wrong. There's something weird with the range() call.

Ah, I know what the problem is.

If your input were "1000", your range would result to "range(-1, -5)"

The range() function uses range(start, stop, step)

Because the start is greater than the stop, the loop never happens. You need to include a step number. "range(-1, -5, -1)". This way your loop will go backwards until it reaches -5. Otherwise your start is greater than your stop, so the loop never runs.
Reply


Messages In This Thread
Python - by Manu - 2011-03-30, 03:19 PM
Python - by Fiel - 2011-03-30, 03:40 PM
Python - by Manu - 2011-03-30, 03:54 PM
Python - by Fiel - 2011-03-30, 04:04 PM
Python - by Manu - 2011-03-30, 07:03 PM
Python - by Fiel - 2011-03-30, 07:16 PM
Python - by Manu - 2011-03-30, 08:11 PM
Python - by Manu - 2011-03-31, 08:16 PM
Python - by Fiel - 2011-04-01, 03:12 AM
Python - by Manu - 2011-04-01, 08:27 PM
Python - by Fiel - 2011-04-01, 08:38 PM

Forum Jump:


Users browsing this thread: