英文:
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
问题
Test Case1:
Input: 987o35l7o4
Output: 3 mistakes 9870351704
Test Case2:
Input: 9874618291
Output: No mistakes
英文:
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")
答案1
得分: 0
代码中存在三个基本错误,我已经进行了修正并在下面进行了标注:
n1 = input()
n1 = n1.lower() # 需要使用 () 调用 lower() 方法
mistake = 0
if len(n1) == 10:
for a in n1: # 遍历 n1 中的字符
if a == "o" or a == "i":
mistake += 1
print(mistake, "mistakes") # 等到循环结束再打印错误总数
else:
print("输入值必须为10位数字")
n1.lower
只会给你lower
方法本身,你需要使用()
来实际调用它并获得小写字符串。for a in range(len(n1))
会给你索引,但你想要遍历实际字符,只需使用for a in n1
。- 等待循环结束再打印错误的数量!
你仍然需要解决将输入字符串更正为完全数值形式的替代项,并且还有一个错误我没有指出(尝试复制并粘贴测试用例,你将很快发现它),但希望上述内容能帮助你解决你当前遇到的问题,并更接近解决方案。
英文:
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 thelower
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 doingfor 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.
答案2
得分: 0
n1 = input()
n1 = n1.lower() # 这里需要放()
n1=[*n1] # 将n1转换为列表
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": # 这里你需要放l而不是i
mistake += 1
n1[a] = '1'
if mistake == 0:
print("没有错误")
else:
print(mistake)
print(int(''.join(n1)))
else:
print("输入值必须是10位数字")
英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论