Hey there! Sign in to join this conversationNew here? Join for free
x Turn on thread page Beta

Computer Science - Python. Append replaces the elements in the list. watch

    • Thread Starter
    Offline

    1
    ReputationRep:
    I have created a function which creates an empty list, and assigns a value from another list to a variable. Then the code is supposed to append the variable's value to the list each time the function is called. But the function doesn't append, but instead replaces the element inside the list. I can tell this because when I print the list, the previous element is missing. The code is below. I need this for a mock examination. Any ideas?

    def track():
    global tracker
    global trackerresult
    trackerresult = []
    tracker = opposite1[decider]
    trackerresult.append(tracker)
    print(trackerresult)
    Offline

    20
    ReputationRep:
    (Original post by iRealizediKing)
    I have created a function which creates an empty list, and assigns a value from another list to a variable. Then the code is supposed to append the variable's value to the list each time the function is called. But the function doesn't append, but instead replaces the element inside the list. I can tell this because when I print the list, the previous element is missing. The code is below. I need this for a mock examination. Any ideas?

    def track():
    global tracker
    global trackerresult
    trackerresult = []
    tracker = opposite1[decider]
    trackerresult.append(tracker)
    print(trackerresult)
    trackerresult is empty because you've set it to empty two lines above. You're calling append on an empty list.

    In future, it might be a good idea to put a print statement above the line you're unsure of to check the values of any relevant variables so that you can see what's happening. Or use a debugger.
    Offline

    11
    ReputationRep:
    Morgan is right. Solution is to define your variable outside of the function, and then place it as a parameter inside the brackets.

    e.g.:

    trackerresult = []

    def track(trackerresult):
    etc.
    • Thread Starter
    Offline

    1
    ReputationRep:
    Thanks guys for all your replies. I was able to solve the problem by defining the variable outside the function and ensuring that its global.
    I completed the assignment and score A* in my entire controlled assessment! God bless you all!
    Offline

    2
    ReputationRep:
    You do realise you could be done for plagiarism now this code can be viewed by anyone?
    (Original post by iRealizediKing)
    Thanks guys for all your replies. I was able to solve the problem by defining the variable outside the function and ensuring that its global.
    I completed the assignment and score A* in my entire controlled assessment! God bless you all!
    Offline

    13
    ReputationRep:
    (Original post by Will98765)
    You do realise you could be done for plagiarism now this code can be viewed by anyone?
    You do realise that's not how intellectual property rights work? You also realise that asking for help debugging <1% of their code doesn't constitute plagiarism in the same way that looking up the correct spelling of a word in the dictionary would not constitute plagiarism in a poem?

    Just asking.
    Offline

    11
    ReputationRep:
    (Original post by Will98765)
    You do realise you could be done for plagiarism now this code can be viewed by anyone?
    There's not a lot of code there, it's just a small snippet. The controlled assessment is a lot more than just that.
    • Thread Starter
    Offline

    1
    ReputationRep:
    (Original post by Will98765)
    You do realise you could be done for plagiarism now this code can be viewed by anyone?
    I only needed some minor support on solving a small part of an error in a piece of code, and if I had enough time I probably would've debugged it myself. Its not like I asked for the controlled assessment to be done for me, on a whole the assessment was much much larger, and the overall mark I achieved would have only been very slightly affected by that one piece of code. Just like dictionaries exist.

    Juts clarifying.
    Offline

    18
    ReputationRep:
    Hi everyone, are any of you doing the A453 Computing controlled assessment with the arithmetic quiz? . i am completely stumped for task 3 programming and any help would be very greatly appreciated!!!
    • Thread Starter
    Offline

    1
    ReputationRep:
    (Original post by sunshine774)
    Hi everyone, are any of you doing the A453 Computing controlled assessment with the arithmetic quiz? . i am completely stumped for task 3 programming and any help would be very greatly appreciated!!!
    I am not currently doing it but I can help and give hints if you tell me what the task is and what your having issues with?
    Offline

    18
    ReputationRep:
    (Original post by iRealizediKing)
    I am not currently doing it but I can help and give hints if you tell me what the task is and what your having issues with?
    hi hope you are fine. the task is set by OCR. This is task 3 and i cant seem to program it...

    Task 3: The teacher wants to use the results from students taking these quizzes to log their performance. The system should store the last three scores for each student. The teacher would like to be able to output the results of the quiz for a particular class, sorted:

    :• in alphabetical order with each student’s highest score for the tests
    • by the highest score, highest to lowest
    • by the average score, highest to lowest.

    Analyse the requirements in detail for this program and design, code, test and evaluate a program that will allow the teacher to select which class group to look at and which field to use when sorting the output data

    I am just VERY unsure on how to use lists and dictionaries to do what it says in the task...
    Offline

    11
    ReputationRep:
    (Original post by sunshine774)
    hi hope you are fine. the task is set by OCR. This is task 3 and i cant seem to program it...

    Task 3: The teacher wants to use the results from students taking these quizzes to log their performance. The system should store the last three scores for each student. The teacher would like to be able to output the results of the quiz for a particular class, sorted:

    :• in alphabetical order with each student’s highest score for the tests
    • by the highest score, highest to lowest
    • by the average score, highest to lowest.

    Analyse the requirements in detail for this program and design, code, test and evaluate a program that will allow the teacher to select which class group to look at and which field to use when sorting the output data

    I am just VERY unsure on how to use lists and dictionaries to do what it says in the task...
    Python has a big range of sorting functions which you can use. I'd advise you to check the python documentation on lists and dictionaries.

    I'd guess you need to store the scores in a dictionary or multidimensional list through which you can sort and output. If you haven't done so already, make a flowchart or pseudocode so you have a general idea of what to code.
    Posted from TSR Mobile
 
 
 
Reply
Submit reply
Turn on thread page Beta
Updated: October 27, 2015
Poll
Do you agree with the proposed ban on plastic straws and cotton buds?
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

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