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

iRealizediKing
Badges: 1
Rep:
?
#1
Report Thread starter 4 years ago
#1
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)
0
reply
username1560589
Badges: 20
Rep:
?
#2
Report 4 years ago
#2
(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.
0
reply
habeeb_00
Badges: 11
Rep:
?
#3
Report 4 years ago
#3
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.
0
reply
iRealizediKing
Badges: 1
Rep:
?
#4
Report Thread starter 4 years ago
#4
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!
1
reply
Will98765
Badges: 8
Rep:
?
#5
Report 4 years ago
#5
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!
0
reply
Duskstar
Badges: 13
Rep:
?
#6
Report 4 years ago
#6
(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.
0
reply
habeeb_00
Badges: 11
Rep:
?
#7
Report 4 years ago
#7
(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.
0
reply
iRealizediKing
Badges: 1
Rep:
?
#8
Report Thread starter 4 years ago
#8
(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.
0
reply
sunshine774
Badges: 18
Rep:
?
#9
Report 4 years ago
#9
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!!!
0
reply
iRealizediKing
Badges: 1
Rep:
?
#10
Report Thread starter 4 years ago
#10
(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?
0
reply
sunshine774
Badges: 18
Rep:
?
#11
Report 4 years ago
#11
(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...
0
reply
habeeb_00
Badges: 11
Rep:
?
#12
Report 4 years ago
#12
(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
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • Bournemouth University
    Undergraduate Open Day Undergraduate
    Wed, 19 Feb '20
  • Buckinghamshire New University
    Postgraduate and professional courses Postgraduate
    Wed, 19 Feb '20
  • University of Warwick
    Warwick Business School Postgraduate
    Thu, 20 Feb '20

Has your university offer been reduced?

Yes (45)
33.09%
No (69)
50.74%
Don't know (22)
16.18%

Watched Threads

View All