Strange error: can´t decode a byte. Help please!


#1



https://www.codecademy.com/es/courses/python-beginner-en-7s5jh/1/3?curriculum_id=53a32847fed2a88075000001


Traceback (most recent call last):
File "python", line 32, in
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 23: ordinal not in range(128)


I don`t know why it doesn´t work. I hope u can help me. Thanks!


import random

tablero = []

for x in range(0,5):
  tablero.append(["O"] * 5)

def print_tablero(tablero):
  for fila in tablero:
    print " ".join(fila)

print_tablero(tablero)

def fila_aleatoria(tablero):
  return random.randint(0,len(tablero)-1)

def columna_aleatoria(tablero):
	return random.randint(0,len(tablero[0])-1)

barco_fila = fila_aleatoria(tablero)
barco_columna = columna_aleatoria(tablero)
adivina_fila = input("Adivina fila:")
adivina_columna = input("Adivina columna:")

print barco_fila
print barco_columna

# escribí tu código a continuación.
if adivina_fila==barco_fila:
    print "Felicitaciones, hundiste mi barco!"
elif adivina_fila != range(5) or adivina_columna != range(5):
     print "Huy eso ni siquiera está en el oceano"
else :
    print "Agua, no tocaste mi barco"
    adivina_fila='X'
    adivina_columna='X'


#2

Problem is here:

print "Huy eso ni siquiera está en el oceano"

Python does not work well with the unicode characters. So you can change á to a:

print "Huy eso ni siquiera esta en el oceano"

Or you can define this string as a unicode string, you just need to add u at the beginning:

print u"Huy eso ni siquiera está en el oceano"

#3

There is a non-ASCII character that is causing the error.

There are two things you can do:

  1. To pass, don't enter a number greater that 4 or less than 0 for either guess; or,
  2. preface the string with u. This option will also pass since there is no error.

print u"Huy eso ni siquiera está en el oceano"

to signal Python to expect Unicode. 0xc3 is ordinal 194.


#4

elif adivina_fila != range(5) or adivina_columna != range(5):

apparently the probem is the sintax. In a range i can´t write !=, I have to write not in. Thank you everyone


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.