How to make a python program that lists the positions and displays and error message if not found

Anonymous User

I did this code:

sentence = input("Type in your sentance ").lower().split()
Word = input("What word would you like to find? ")
Keyword = Word.lower().split().append(Word)
positions = []

for (S, subword) in enumerate(sentence):
    if (subword == Word):
        positions.append
        print("The word" , Word , "is in position" , S+1)

But there are 2 problems with it; I dont know how to write a code when the users word is not found and to but the positions in "The word position is in [1,3,6,9]. Any help? Thanks

Anonymous

Your code is having multiple errors. I am pasting here the sample code for your reference:

from collections import defaultdict

sentence_string = raw_input('Enter Sentence: ')
# Enter Sentence: Here is the content I need to check for index of all words as Yes Hello Yes Yes Hello Yes

word_string = raw_input("Enter Words: ")
# Enter Words: yes hello

word_list = sentence_string.lower().split()
words = word_string.lower().split()

my_dict = defaultdict(list)
for i, word in enumerate(word_list):
     my_dict[word].append(i)

for word in words:
    print "The word" , word, "is in position " , my_dict[word]

# The word yes is in position  [21, 23, 24, 26]
# The word hello is in position  [22, 25]

The approach here is:

  1. Break your sentence i.e sentence_string here into list of words
  2. Break your word string into list of words.
  3. Create a dictionary my_dict to store all the indexes of the words in word_list
  4. Iterate over the words to get your result with index, based on the value you store in my_dict.

Note: The commented part in above example is basically the output of the code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Message error 'C:/Program' not found with glmmadmb() in R

Error: Program "make" not found in PATH in QNX Momentics

How to make this program in python?

Error running Python program: Demystify error message

How do you create a Custom Validator that displays a custom error message?

how do i prevent error message that displays confidential data in laravel

How to make a poll in python with lists

How to make program display an error message if the input doesn't start with a letter

How would I make this Python program with lists/arrays Instead of multiple variables?

I found an error message in my program using SQlite with android studio

The result not found message displays with the searched data

Certain commands not working : make, wget - error message command not found

How to make fuzzy search between lists showing matches and not found elements?

How to make the program continue/restart when there is an Error?

How to not make the program restart after a value error?

python: eliminate positions of nan in multiple lists

How to make error message in TextInputLayout appear in center

how to make ionic popup error message on submit

how to make a dynamic error message in django form

How to Make A Combination of Lists of A List in Python

How to make a matrix with elements that are lists in python

How to make a dict of multiple lists in python?

How to make a json from two lists in Python

How to make elasticsearch query with Python lists

How to make lists equal a quadrilateral in Python?

How to substitute specific positions using regex and lists?

AEM: 'No Design' message displays in the error logs

Php validation always displays error message

Program displays random lists data when reading from file

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive