Python Forum
Need help explaining what the variables do in my code.
Thread Rating:
  • 3 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help explaining what the variables do in my code.
#1
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
[Image: ?ui=2&ik=635a3fea80&view=fimg&th=159d532...&zw&atsh=1]
Reply
#2
post your code in python code tags, not a screenshot of it.
Recommended Tutorials:
Reply
#3
(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.
Reply
#4
(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")
Reply
#5
What have you tried writing? Do you have any specific questions?
Reply
#6
(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
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Explaining "translate" method lummers 4 2,356 Jan-13-2020, 06:31 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020