Issue
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:
Solution
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)
Answered By - Shodai Thox
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.