A faulty keyboard where 0 and 1 sometime doesn't work is replaced by "o" and "i" respectively find the mistakes in the input and correct it

Question:

A data entry operator has a faulty keyboard. The keys 0 and 1 are very unreliable. Sometimes they work, sometimes they don’t. While entering phone numbers into a database, the operator uses the letter ‘l’ as a replacement for 1 and ‘o’ as a replacement for 0 whenever these binary digits let him down. Both ‘l’ and ‘o’ are in lower case. ‘l’ is the first letter of the word ‘land’, and not capital ‘i’.

Accept a ten-digit number as input. Find the number of places where the numbers 0 and 1 have been replaced by letters. If there are no such replacements, print the string No mistakes. If not, print the number of mistakes (replacements) and in the next line, print the correct phone number.

Test Case1:
Input: 987o35l7o4
Output:3 mistakes 9870351704

Test Case2:
Input: 9874618291
Output: No mistakes

n1 = input()
n1 = n1.lower
mistake = 0
if len(n1) == 10:
    for a in range(0,len(n1)):
        if a == "o" or a == "i":
            mistake += 1
            print(mistake,"mistakes")
        elif a == 0 or a ==1:
            print("No mistakes")
else:
    print("The input value must be a 10 digit")
Asked By: KRUSHANU BHATT

||

Answers:

There are three basic mistakes in the code you have so far, which I’ve fixed and called out below:

n1 = input()
n1 = n1.lower()  # need () to call the lower() method
mistake = 0
if len(n1) == 10:
    for a in n1:  # iterate over the characters in n1
        if a == "o" or a == "i":
            mistake += 1
    print(mistake,"mistakes")  # wait until the end to print the total
else:
    print("The input value must be a 10 digit")
  • n1.lower just gives you the lower method itself; you need () to actually call it and get the lowercase string.
  • for a in range(len(n1)) gives you indices, but you want to iterate over the actual characters by just doing for a in n1.
  • wait until the end of the loop to print out the number of mistakes!

You still need to address the substitutions to correct the input string into the fully numeric form, and there is one more bug that I haven’t called out (try copying and pasting the test cases and you’ll find it very quickly) but hopefully the above gets you over the immediate problems you’re encountering and a bit closer to a solution.

Answered By: Samwise
n1 = input()
n1 = n1.lower()  # here you need to put ()
n1=[*n1]  # convert n1 into a list
mistake = 0
if len(n1) == 10:
    for a in range(0,len(n1)):
        if n1[a] == "o" :
            mistake += 1
            n1[a] = '0'
        elif n1[a] == "l":   # here you have to put l instead of i
            mistake += 1
            n1[a] = '1'
    if mistake ==0:
        print("No mistakes")
    else:
        print(mistake)
        print(int(''.join(n1)))
else:
    print("The input value must be a 10 digit")
Answered By: God Is One
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.