This is a simple console application that just gives a phone number and writes it by letters, but the problem is that you need to enter a number first and it starts working after the first number.
print("---Give numbers by letters---" "\n")
#number=input("Phone: ")
number_convertor={
"0":"oh",
"1":"one",
"2":"two",
"3":"three",
"4":"four",
"5":"five",
"6":"six",
"7":"seven",
"8":"eight",
"9":"nine"
}
while True:
result=""
number=input("Phone: ")
for ch in number:
result =number_convertor.get(ch,"!") " "
print(result)
Output:
---Give numbers by letters---
Phone: 0
Phone: 0
oh
Phone: 0
oh
Phone: 0
oh
Phone:
and when I remove the 2th line, is prints multiple times!
---Give numbers by letters---
Phone: 021
oh
oh two
oh two one
Phone:
CodePudding user response:
1-You were prompting the user to enter a number 2 times
2- The final print doesn't have to be within the for loop, but outside it.
Try this, and you don't need the while loop.
print("---Give numbers by letters---" "\n")
number_convertor={
"0":"oh",
"1":"one",
"2":"two",
"3":"three",
"4":"four",
"5":"five",
"6":"six",
"7":"seven",
"8":"eight",
"9":"nine"
}
result=""
number=input("Phone: ")
for ch in number:
result =number_convertor.get(ch,"!") " "
print(result)
CodePudding user response:
There are two bugs; you discard the result from the first input
, and you repeatedly print result
while producing it.
There is no need to collect anything into result
anyway if you just want to print one number at a time.
The first print
probably makes more sense to perform after the initialization of the dictionary, together with the other I/O. I also propose a simplification of the initialization.
numbers = "oh one two three four five" \
" six seven eight nine ten".split()
print("---Give numbers by letters---")
while True:
sep = ""
number = input("Phone: ")
for ch in number:
print("%s%s" % (sep, numbers[int(ch)]), end=sep)
sep = " "
print()