2011-03-30, 03:40 PM
Sería mejor hacerlo así:
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.
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.
