我正在尝试编写一个程序,提示用户输入一个字符串的电话号码。输入可以包含字母,程序将字母(无论是大写还是小写)转换为数字,并使所有其他字符保持不变。这是我到目前为止所做的:
def getNumber(uppercaseLetter):
if uppercaseLetter == 'A':
number = 2
elif uppercaseLetter == 'B':
number = 2
elif uppercaseLetter == 'C':
number = 2
elif uppercaseLetter == 'D':
number = 3
elif uppercaseLetter == 'E':
number = 3
elif uppercaseLetter == 'F':
number = 3
elif uppercaseLetter == 'G':
number = 4
elif uppercaseLetter == 'H':
number = 4
elif uppercaseLetter == 'I':
number = 4
elif uppercaseLetter == 'J':
number = 5
elif uppercaseLetter == 'K':
number = 5
elif uppercaseLetter == 'L':
number = 5
elif uppercaseLetter == 'M':
number = 6
elif uppercaseLetter == 'N':
number = 6
elif uppercaseLetter == 'O':
number = 6
elif uppercaseLetter == 'P':
number = 7
elif uppercaseLetter == 'Q':
number = 7
elif uppercaseLetter == 'R':
number = 7
elif uppercaseLetter == 'S':
number = 7
elif uppercaseLetter == 'T':
number = 8
elif uppercaseLetter == 'U':
number = 8
elif uppercaseLetter == 'V':
number = 8
elif uppercaseLetter == 'W':
number = 9
elif uppercaseLetter == 'X':
number = 9
elif uppercaseLetter == 'Y':
number = 9
elif uppercaseLetter == 'Z':
number = 9
return number
def translateNumber():
phoneNumber = str(input("Enter a string: "))
for char in phoneNumber:
if char in ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']:
getNumber(char)
return char
else:
return char
print(phoneNumber)
出于某种原因,我无法让它发挥作用,我不确定问题是什么。有人可以帮我解决这个问题吗?谢谢!
答案 0 :(得分:5)
不解释您的代码(已由Chrisitan完成)。这只是一种方式:
>>> char_numbers = [('abc',2), ('def',3), ('ghi',4), ('jkl',5), ('mno',6), ('pqrs',7), ('tuv',8), ('wxyz',9)]
>>> char_num_map = {c:v for k,v in char_numbers for c in k}
>>> phone = "JUST 1 NUMBER"
>>> result = "".join(str(char_num_map.get(v,v)) for v in phone.lower())
>>> result
'5878 1 686237'
答案 1 :(得分:3)
您的函数getNumber()
会返回一个数字,但您不会存储它。要存储它,请将结果分配给变量,然后将其返回:
def translateNumber():
phoneNumber = str(input("Enter a string: "))
for char in phoneNumber:
if char in ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']:
result = getNumber(char)
return result
else:
return char
答案 2 :(得分:0)
def getNumber(uppercaseLetter):
#Create a function to return phone number
ascii = ord(uppercaseLetter) # get ascii using ord() method
# returns all the possible digits
if ascii < 83:
Number = (ascii - 65)//3+2
elif ascii == 83:
Number = 7
elif ascii >83 and ascii < 97:
Number = 8
#If the input char is a digit, returns the digit
elif isdigit(uppercaseLetter) == True:
Number = uppercaseLetter
else:
Number = 9
Number = str(Number)
return Number
def main(): # Define the main function
output = "" # Setting the output as a blank string
str = input('Enter a string: \n') # Get input from user
lst = [x for x in str] # convert all char in str to a list
for x in str:
# If the char x is alphabet make it to the uppercase
if isdigit(x) == False and isalnum(x) == True:
uppercaseLetter = x.upper()
result = getNumber(uppercaseLetter)
output = output + result
if isalnum(x) == False: # If the char x is a digit
output = output + x
else:
output = output + x
print(output)
main() # Excute the main function