Posts: 5
Threads: 2
Joined: Jan 2017
Jan-26-2017, 09:04 PM
(This post was last modified: Jan-26-2017, 09:04 PM by BurnedChipotle.)
Hello I'm learning Python at school and have been set a task,
Task 1
Develop a program that analyses a sentence that contains several words without punctuation. When
a word in that sentence is input, the program identifies all of the positions where the word occurs in
the sentence. The system should not be case sensitive: Ask, ask, ASK should be treated as the same
word.
For example, in the sentence
ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR
COUNTRY
The word ‘COUNTRY’ occurs in the 5th and 17th positions.
Analyse the requirements for this system and design, develop, test and evaluate a program to locate
and return the position(s) of the word you have selected in a particular sentence or return an error
message if the word is not in the sentence.
however my teacher has been off for the past 1-2 weeks due to a bad back and therefore I have had multiple supply teachers. I have written my code and it is working but now I have to do a write up explaining what my the variables are in my code and what they do, can somebody explain to me why am I using these certain variables or give me advice on what to write? Thank you
This is a screenshot of my code so far
Posts: 5,151
Threads: 396
Joined: Sep 2016
post your code in python code tags, not a screenshot of it.
Recommended Tutorials:
Posts: 2,342
Threads: 62
Joined: Sep 2016
(Jan-26-2017, 09:04 PM)BurnedChipotle Wrote: have to do a write up explaining what my the variables are in my code and what they do, can somebody explain to me why am I using these certain variables or give me advice on what to write? Please don't take this as an accusation, but when someone says something like this my first thought is, "they found this code online and need someone to explain it to them." At the very least, you should be asking more specific questions or making an attempt and asking us to point out problems with that attempt. Generally, we aren't going to do anyone's homework for them, even if they've already succeeded with part of that homework.
Also, metulburr is right that you should be using code tags but in any case your image link looks broken to me.
Posts: 5
Threads: 2
Joined: Jan 2017
Jan-30-2017, 10:01 PM
(This post was last modified: Jan-30-2017, 10:10 PM by BurnedChipotle.)
(Jan-26-2017, 09:45 PM)micseydel Wrote: (Jan-26-2017, 09:04 PM)BurnedChipotle Wrote: have to do a write up explaining what my the variables are in my code and what they do, can somebody explain to me why am I using these certain variables or give me advice on what to write? Please don't take this as an accusation, but when someone says something like this my first thought is, "they found this code online and need someone to explain it to them." At the very least, you should be asking more specific questions or making an attempt and asking us to point out problems with that attempt. Generally, we aren't going to do anyone's homework for them, even if they've already succeeded with part of that homework.
Also, metulburr is right that you should be using code tags but in any case your image link looks broken to me. I understand what you mean, I can assure that I have written this coding in my lessons however now that I have moved on to the write up section I struggle with explaining it as I've only just started learning Python this year. I just need to know how the Position variable, which I now know is an Integer variable works in my code and also how the words=sentence.split(" ") variable works.
My coding is below
sentence=input ("type in the list you wish to be counted ").lower()
keyword=input ("enter the word to be searched ").lower()
words=sentence.split(" ")
position=1
for word in words:
if word==keyword:
print ("found at {0}".format (position))
position=position+1
if not keyword in words:
print ("Error: word was not found in list")
Posts: 2,342
Threads: 62
Joined: Sep 2016
What have you tried writing? Do you have any specific questions?
Posts: 5
Threads: 2
Joined: Jan 2017
Jan-30-2017, 10:13 PM
(This post was last modified: Jan-30-2017, 10:13 PM by BurnedChipotle.)
(Jan-30-2017, 10:07 PM)micseydel Wrote: What have you tried writing? Do you have any specific questions?
So far I have explained the first two variables I have created in my coding, this is what I have wrote.
Sentence is the first variable that will be created, this variable represents the sentence that will be inputted and searched by the program, the variable allows the user to input a sentence/wordlist into the system this is a string variable because it can be attached onto different other strings which the can be added into a list with the other strings.
Keyword is my second variable this is assigned to a word that the user has inputted to search for. This variable will be created in order for the user to find any word that was previously entered in the sentence before. This is a string variable
I don't understand how the Position variable works and how the word variable works
Posts: 7,320
Threads: 123
Joined: Sep 2016
Here is a hint.
>>> words = ['car', 'bus', 'taxi']
>>> for index,item in enumerate(words):
... print('At index {} is item {}'.format(index,item))
...
At index 0 is item car
At index 1 is item bus
At index 2 is item taxi
|