Tenemos una actividad escolar donde necesito asignar el alfabeto a los números de teléfono. Ya casi termino pero parece que no puedo lograr que los espacios de una oración se conviertan en "-"

El código que he probado es este:

sentence = input("Enter a sentence: ").upper()

phone_number = ""

for number in sentence:

if(number.isalpha()):
    if (number in "ABC"):
        phone_number += "2"
    elif (number in "DEF"):
        phone_number += "3"
    elif (number in "GHI"):
        phone_number += "4"
    elif (number in "JKL"):
        phone_number += "5"
    elif (number in "MNO"):
        phone_number += "6"
    elif (number in "PQRS"):
        phone_number += "7"
    elif (number in "TUV"):
        phone_number += "8"
    elif (number in "WXYZ"):
        phone_number += "9"
    else:
        phone_number += number

   number_sequence = phone_number.replace(" ","-")

   print(number_sequence)

Esperaba que el espacio de una oración se reemplazara con "-" como si escribiera "jaja jaja" debe ser 4242-4242, pero el guión / guión no aparece.

0
helloplshelp 3 oct. 2019 a las 14:58

3 respuestas

La mejor respuesta

Su instrucción if isalpha () no reconoce una cadena vacía en un alfabeto. Simplemente cree una declaración else al final así:

sentence = input("Enter a sentence: ").upper()

phone_number = ""

for char in sentence:

    if(char.isalpha()):

        if (char in "ABC"):
            phone_number += "2"
        elif (char in "DEF"):
            phone_number += "3"
        elif (char in "GHI"):
            phone_number += "4"
        elif (char in "JKL"):
            phone_number += "5"
        elif (char in "MNO"):
            phone_number += "6"
        elif (char in "PQRS"):
            phone_number += "7"
        elif (char in "TUV"):
            phone_number += "8"
        elif (char in "WXYZ"):
            phone_number += "9"
        else:
            phone_number += char

    elif char in ' ':
        phone_number += '-'
    else:
        print('Error. Please dont use special characters.')

print(number_sequence)
1
Heini 3 oct. 2019 a las 12:15
sentence = input("Enter a sentence: ").upper()

phone_number = ""

for number in sentence:

    if (number in "ABC"):
            phone_number += "2"
    elif (number in "DEF"):
            phone_number += "3"
    elif (number in "GHI"):
            phone_number += "4"
    elif (number in "JKL"):
            phone_number += "5"
    elif (number in "MNO"):
            phone_number += "6"
    elif (number in "PQRS"):
            phone_number += "7"
    elif (number in "TUV"):
            phone_number += "8"
    elif (number in "WXYZ"):
            phone_number += "9"
    else:
            phone_number += number

number_sequence = phone_number.replace(" ","-")
print(number_sequence)

Su primera declaración 'if' estaba verificando si el elemento de cadena es un alfabeto, que no es el caso de un espacio (''), eliminarlo e indentar correctamente su código resolvió el problema.

1
Trollsors 3 oct. 2019 a las 12:38

.isalpha () devuelve False para el espacio, por lo que lo está perdiendo en su primer 'if'

if(number.isalpha() or number==' '):

Esto resolvería el problema. O:

Puede eliminar la última intención:

sentence = input("Enter a sentence: ").upper()

phone_number = ""

for number in sentence:

  if(number.isalpha()):
      if (number in "ABC"):
          phone_number += "2"
      elif (number in "DEF"):
          phone_number += "3"
      elif (number in "GHI"):
          phone_number += "4"
      elif (number in "JKL"):
          phone_number += "5"
      elif (number in "MNO"):
          phone_number += "6"
      elif (number in "PQRS"):
          phone_number += "7"
      elif (number in "TUV"):
          phone_number += "8"
      elif (number in "WXYZ"):
          phone_number += "9"
  else:
      phone_number += number

number_sequence = phone_number.replace(" ","-")

print(number_sequence)
0
pozniejzmienie 3 oct. 2019 a las 12:16
58218637