Home > Blockchain >  Python 3: Phone number from input with characters
Python 3: Phone number from input with characters

Time:05-16

I keep getting an indentation error when submitting my programing homework in mimir.ide enter image description here

Here is my code:

number = input("Please enter a phone number:")
characters = number.split('-')
AC = characters[0]
NC = characters[1:]
value =''
for n in NC:
for i in range (len(n)):
     if n[i] in 'ABC':
            value = value  '2'
        elif n[i] in 'DEF':
                value = value  '3'
        elif n[i] in 'GHI':
                value = value '4'
        elif n[i] in 'JKL':
                value = value '5'
        elif n[i] in 'MNO':
                value = value '6'
        elif n[i] in 'PQRS':
                value = value '7'
        elif n[i] in 'TUV':
                value = value '8'
        elif n[i] in 'WXY':
                value = value '8'
    value= value  '-'
return AC   '-'   value[:-1]
new_number = phone_number(number)
print("Input number:",number)
print("New number: ",new_number)

CodePudding user response:

  • The inner for needs to be indented relative to the outer for
  • The elifs need to be on the same indentation as the if.
  • The final value= line needs to be on the same indentation as one of the two above. Which one depends on what exactly you want the code to do.
  • return only works inside a function. Your code doesn't contain a function.

CodePudding user response:

elifs need to be indented same way as if also after for n in NC: you are missing indentation, and finally you didn't define function (I am guessing you wanted to name it phone_number). Here is the fixed version of your code.

number = input("Please enter a phone number:")
characters = number.split('-')
def phone_number(num):
   AC = characters[0]
   NC = characters[1:]
   value = ''
   for n in NC:
       for i in range(len(n)):
           if n[i] in 'ABC':
                   value = value   '2'
           elif n[i] in 'DEF':
                   value = value  '3'
           elif n[i] in 'GHI':
                   value = value '4'
           elif n[i] in 'JKL':
                   value = value '5'
           elif n[i] in 'MNO':
                   value = value '6'
           elif n[i] in 'PQRS':
                   value = value '7'
           elif n[i] in 'TUV':
                   value = value '8'
           elif n[i] in 'WXY':
                   value = value '8'
       value= value  '-'
   return AC   '-'   value[:-1]
new_number = phone_number(number)
print("Input number:",number)
print("New number: ",new_number)
  • Related