How to change integer in a sentence to pharses | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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)

17th Sep 2023, 5:03 PM
dido carter
dido carter - avatar
17 Answers
+ 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 👍
17th Sep 2023, 6:22 PM
Keith
Keith - avatar
+ 6
Riya , Mafdi it is not seen as a helpful behavior when we are going to post a ready-made, as long as the op has not shown his attempt here. it is helpful to give hints and tips, so that the op has a chance to find a solution by himself.
18th Sep 2023, 10:00 AM
Lothar
Lothar - avatar
+ 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.
17th Sep 2023, 5:14 PM
JAY
JAY - avatar
+ 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
17th Sep 2023, 6:14 PM
Keith
Keith - avatar
+ 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
17th Sep 2023, 6:42 PM
Mafdi
Mafdi - avatar
+ 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))
17th Sep 2023, 5:59 PM
Mafdi
Mafdi - avatar
+ 1
# python addition assignment operator text = "hello" text += " world" print(text) # hello world num = 2 num += 5 print(num) # 7
17th Sep 2023, 6:33 PM
Mafdi
Mafdi - avatar
+ 1
Lothar you are right ,but I made my try but was failed , the other friends helped me to understand better man ,
18th Sep 2023, 10:05 AM
dido carter
dido carter - avatar
+ 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
18th Sep 2023, 2:21 PM
Mafdi
Mafdi - avatar
+ 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".
19th Sep 2023, 10:52 AM
Mafdi
Mafdi - avatar
0
Can you please explain your line 3 ?? Or can you say my peoblems in coding thank you
17th Sep 2023, 5:24 PM
dido carter
dido carter - avatar
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
17th Sep 2023, 5:46 PM
JAY
JAY - avatar
0
Keith Driscoll what do you mean by removing text , it would be better if we mebtion text , not?
17th Sep 2023, 6:15 PM
dido carter
dido carter - avatar
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
17th Sep 2023, 6:18 PM
dido carter
dido carter - avatar
0
Mafdi thank you so much for this great help
18th Sep 2023, 10:05 AM
dido carter
dido carter - avatar
0
Hola
19th Sep 2023, 4:50 AM
Andres Solano
0
Mafdi I save the code but I dont know how can I access them again ,can you tell me tnx
19th Sep 2023, 6:34 AM
dido carter
dido carter - avatar