Can someone pleasee explain this python code??

Announcements Posted on
Four things that unis think matter more than league tables 08-12-2016
    • Thread Starter
    Offline

    2
    ReputationRep:
    So I need to explain how this code works and what each part does but I'm soo baffed. Help Please?
    Here's the code:
    Sentence = input("Enter a sentence!")
    s = Sentence.split()
    another = [0]

    for count, i in enumerate(s):
    if s.count(i) < 2:
    another.append(max(another) + 1)
    else:
    another.append(s.index(i) +1)

    another.remove(0)

    print(another)
    Offline

    3
    ReputationRep:
    Have you actually played with the code? If you don't have an IDE installed you could play with it somewhere like here https://repl.it/languages/python3

    Let me know if you still get stuck on anything in particular
    Offline

    3
    ReputationRep:
    Which parts of it do you understand, and which are you struggling with?
    Offline

    3
    ReputationRep:
    (Original post by DD2508)
    So I need to explain how this code works and what each part does but I'm soo baffed. Help Please?
    Here's the code:
    Sentence = input("Enter a sentence!"
    s = Sentence.split()
    another = [0]

    for count, i in enumerate(s):
    if s.count(i) < 2:
    another.append(max(another) + 1)
    else:
    another.append(s.index(i) +1)

    another.remove(0)

    print(another)
    What bit do you specifically not understand?
    • Thread Starter
    Offline

    2
    ReputationRep:
    I don't understand the bits with the hashtag:

    Sentence = input("Enter a sentence!")
    s = Sentence, split()
    another = [0]#

    for count, i in enumerate(s)
    if s.count(i) < 2
    another.append(max(another) + 1) #
    else
    another.append(s.index(
    Offline

    3
    ReputationRep:
    Well [ ] designates a list, i.e. [foo, bar]. Does that point you in the right direction?
    Offline

    3
    ReputationRep:
    (Original post by DD2508)
    I don't understand the bits with the hashtag:

    Sentence = input("Enter a sentence!"
    s = Sentence, split()
    another = [0]#

    for count, i in enumerate(s)
    if s.count(i) < 2
    another.append(max(another) + 1) #
    else
    another.append(s.index(
    Enumerate splits an iterable into two: the index of the list (0,1,2,3,4) and the content, which are defined separated by commas.

    Therefore, within the beginning of the for loop, it assigns count to the index of each item with the list, with i with the matching contents.

    With the second hastag, it checks if the number of times i is featured in the iterable is less than two. Else catches it if this is not the case.
    Offline

    3
    ReputationRep:
    (Original post by _gcx)
    Enumerate splits a list into two iterables the index of the list (0,1,2,3,4) and the content, which are defined separated by commas.

    Therefore, within the beginning of the for loop, it queries (count, i) respectively, assigning count to the index of each item with the list, with i with the matching contents.

    With the second hastag, it checks the number of times i is feature in the iterable is less than two. Else catches it if this is not the case.
    You really need to learn how to use proper programming terminology when answering questions.
    Offline

    3
    ReputationRep:
    (Original post by Aklaol)
    You really need to learn how to use proper programming terminology when answering questions.
    How constructive
    Spoiler:
    Show
    It was a single mistake, calm down.
    Offline

    3
    ReputationRep:
    (Original post by _gcx)
    How constructive
    Spoiler:
    Show
    It was a single mistake, calm down.
    Enumerate splits an iterable into two: the index of the list (0,1,2,3,4) and the content, which are defined separated by commas.

    Therefore, within the beginning of the for loop, it queries (count, i) respectively, assigning count to the index of each item with the list, with i with the matching contents.

    With the second hastag, it checks if the number of times i is featured in the iterable is less than two. Else catches it if this is not the case.

    1 - No it doesn't "split" an iterable, it will only return the index of the iterable along with the content of the iterable.

    2 - It doesn't "query", the for loop will simply assign the iterable (count) as a index to each element in the iterable (i).

    3 - It checks how many times a object (in this case i) has occured in a list (in this case s), and if the total is less than 2, then the program will append to the list (another) with the maximum element in the list + 1.

    4 - If the above condition has returned false, then the program will append to the list (another) with the index(taking the ordinal argument of the iterable i) + 1 of the variable s. The program will then remove the first element in the another list, and finally print the another list.

    Either learn how to answer questions properly, or don't answer them at all. You don't need to pretend to understand programming in order to make yourself relevant.
    Offline

    3
    ReputationRep:
    (Original post by Aklaol)
    Enumerate splits an iterable into two: the index of the list (0,1,2,3,4) and the content, which are defined separated by commas.

    Therefore, within the beginning of the for loop, it queries (count, i) respectively, assigning count to the index of each item with the list, with i with the matching contents.

    With the second hastag, it checks if the number of times i is featured in the iterable is less than two. Else catches it if this is not the case.

    1 - No it doesn't "split" an iterable, it will only return the index of the iterable along with the content of the iterable.

    2 - It doesn't "query", the for loop will simply assign the iterable (count) as a index to each element in the iterable (i).

    3 - It checks how many times a object (in this case i) has occured in a list (in this case s), and if the total is less than 2, then the program will append to the list (another) with the maximum element in the list + 1.

    4 - If the above condition has returned false, then the program will append to the list (another) with the index(taking the ordinal argument of the iterable i) + 1 of the variable s. The program will then remove the first element in the another list, and finally print the another list.

    Either learn how to answer questions properly, or don't answer them at all. You don't need to pretend to understand programming in order to make yourself relevant.
    1. That is arguable, if you treat the iterable as [0:blah, 1:blah], then in layman's terms it could be considered splitting, using the definition of "separating into two or more parts" Note that I am not talking about .split here, which is clearly something completely different. (I don't even know how you thought I meant that?)
    2. Point taken, that was some poor (wrong) wording on my part.
    3. That isn't a mistake, that's me not blatantly giving the OP the answer. (to questions they didn't ask, should I add) They were querying the two lines indicated with a #, and were not asking for an explanation of the contents of the loop. Perhaps I should have been more clear.
    4. Above
    Bold: wow that's rough :teehee: No hard feelings, really. I'm not sure if you feel the same, as evidenced from your unjustified bluntness.
    Offline

    3
    ReputationRep:
    (Original post by _gcx)
    1. That is arguable, if you treat the iterable as [0:blah, 1:blah], then in layman's terms it could be considered splitting. Note that I am not talking about .split here.
    2. Point taken, that was some poor (wrong) wording on my part.
    3. That isn't a mistake, that's me not blatantly giving the OP the answer. They were querying the two lines indicated with a #, and were not asking for an explanation of the contents of the loop. Perhaps I should have been more clear.
    4. Above
    Bold: wow that's rough :teehee: No hard feelings, really. I'm not sure if you feel the same, as evidenced from your unjustified bluntness.


    1. It's not splitting, you're beyond delusional if you'd ever consider it to be splitting.

    3 and 4. Your answers were blunt, and lacked key terminology. I also suggest that you look up the definition of querying in respects to computer science.

    The actual problem with you is that you use wrong terminology a lot, maybe you should educate yourself before helping others.
    Offline

    3
    ReputationRep:
    (Original post by Aklaol)
    1. It's not splitting, you're beyond delusional if you'd ever consider it to be splitting.

    3 and 4. Your answers were blunt, and lacked key terminology. I also suggest that you look up the definition of querying in respects to computer science.

    The actual problem with you is that you use wrong terminology a lot, maybe you should educate yourself before helping others.
    There was little more to explain, and I doubt the OP required much more explanation.

    Your points are valid, but you really don't have to use a condescending tone over a few mistakes which don't (with the exception of query, which was a genuine mistake) significantly impact meaning. I am quite receptive to criticism regardless of the way it is comunicated, but others won't be when you speak to them in that tone. Just a bit of advice

    There's no nee to continue this discussion further, if you feel the need to, continue it via PM.
 
 
 
Write a reply… Reply
Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. Oops, you need to agree to our Ts&Cs to register
  2. Slide to join now Processing…

Updated: October 15, 2016
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Poll
Do you think you'll achieve your predicted A Level grades?
Useful resources

Study tools

Rosette

Essay expert

Learn to write like a pro with our ultimate essay guide.

Thinking about uni already?

Thinking about uni already?

See where you can apply with our uni match tool

Student chat

Ask a question

Chat to other GCSE students and get your study questions answered.

Creating

Make study resources

Create all the resources you need to get the grades.

Planner

Create your own Study Plan

Organise all your homework and exams so you never miss another deadline.

Resources by subject

From flashcards to mind maps; there's everything you need for all of your GCSE subjects.

Papers

Find past papers

100s of GCSE past papers for all your subjects at your fingertips.

Help out other students

Can you help? Study help unanswered threads

Groups associated with this forum:

View associated groups

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Quick reply
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.