+ 3
How to change integer in a sentence to pharses
I want to make a code that chagnes integers into pharases in any senteces that will be inputed by user,Like if you write 1 apple and 2 bannana it says : one apple and two bannana, can anyone helo me I have written the folloing codes please correct me : y = input("please write your words") def x {1:one 2:two 3:three 4:four 5:five 6:six 7:seven 8:eight 9:nine} return x; print (y)
17 ответов
+ 2
It will output to the screen and cause the test cases to fail.
If we were writing code for a real world project then we probably would want to prompt the user with some text but that is not the case here 👍
+ 3
1. Define a dictionary mapping integers (1 to 9) to their word forms (e.g., 1 to "one").
2. Split the input sentence into words.
3. Iterate through the words, and for each word:
- Try to convert it to an integer.
- If successful, replace it with its word form from the dictionary.
- If not an integer, leave it as is.
4. Join the modified words back into a sentence.
5. Print the resulting sentence with numbers converted to words.
+ 3
This sounds like the 'No Numerals' Code Coach challenge.
I can give you 2 additional hints:
1. Your dictionary will need to include the number 10.
2. Do not include any 'extra' output when solving Code Coach challenges as it will cause the test cases to fail. In this case change your input statement to:
y = input() # Removed text
+ 3
https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_dictionary_get.asp
get is a dictionary method, it takes one or two arguments (the second is optional)
1st argument is a dictionary key
2nd argument is returned if the first argument is not an existing key in the dictionary.
You can learn all of this and more at Sololearn courses
+ 2
# get user string input
user_input = input("Please enter your text \n")
# define a function that takes a string argument, and returns a new string
def num_dict(text):
# A dictionary to turn the keys on the left, into the values on the right
dict = {"1":"one",
"2":"two",
"3":"three",
"4":"four",
"5":"five",
"6":"six",
"7":"seven",
"8":"eight",
"9":"nine"}
# create an empty string
new_text = ""
# split the string to a list of words and loop through it
for word in text.split():
# add to new_text
# if word is a key in dict get its value,
# otherwise just add word as it is to the new_text
# and don't forget to add a space " " between words
new_text += dict.get(word, word) + " "
return new_text
# call the num_dict function with user_input as argument and print the return value
print(num_dict(user_input))
+ 1
# python addition assignment operator
text = "hello"
text += " world"
print(text) # hello world
num = 2
num += 5
print(num) # 7
+ 1
Lothar you are right ,but I made my try but was failed , the other friends helped me to understand better man ,
+ 1
Lothar you are right I should try a different way
dido carter next time you ask you should save your code and share a link so we can help you better
+ 1
Choose "Create" from the menu below > then choose the "code" tab > from the dropdown menu choose "My code bits".
Also you can choose from the menu at the top right "All" for all languages, and "Python".
0
Can you please explain your line 3 ?? Or can you say my peoblems in coding thank you
0
I hope this will help you.
If you still have any doubt after this, message me.
for i in range(len(words)):
try:
num = int(words[i])
words[i] = number_to_words(num)
except ValueError:
continue
result = " ".join(words)
return result
0
Keith Driscoll what do you mean by removing text , it would be better if we mebtion text , not?
0
Mafdi thanks for this clear information , just can you give me more information about this line:
new_text += dict.get(word, word) + " "
I understood all the rest but I dont know why did you use this line ,and if you can tell me where should I find more explanation regarding this line training please thank you
0
Mafdi thank you so much for this great help
0
Hola
0
Mafdi I save the code but I dont know how can I access them again ,can you tell me tnx