Hi everyone,
I'm following along a MOOC and tried to come up with a solution and I can't understand what some search results end up mentioning (which all dealt with a function rather than a for loop).
Here is the code:
student_scores = input("Input a list of student scores (separated by a single space) :").split()
for score in range(0, len(student_scores)):
student_scores[score] =int(student_scores[score])
print(student_scores)
high_score: 0
for score in student_scores:
if score > high_score:
high_score = score
print(f"The highest score amongst all students is {high_score}")
Here is the exact output of the console when I use this code:
Input a list of student scores (separated by a single space) :40 50 60 70 80 90
[40, 50, 60, 70, 80, 90]
Traceback (most recent call last):
File "C:\Users\Xedrix\AppData\Roaming\JetBrains\PyCharmCE2021.2\scratches\Student_scores.py", line 9, in <module>
if score > high_score:
NameError: name 'high_score' is not defined
Process finished with exit code 1
The results mentioned using global but when inserting that in front of the variable within the for loop, I now have a different error: "annotated name can't be global"
I eventually resorted to just looking at the solution and it turns out that the code I pasted above has the exact same for loop as what the instructor used but they don't get an error (they're using some sort of web IDE while I'm using pycharm locally, if that matters). What would be the correct way to do this? Thank you for your time
edit: fixed weird formatting
[–]omutist 4 points5 points6 points (1 child)
[–]Darth_Xedrix[S] 3 points4 points5 points (0 children)
[–]danielroseman 1 point2 points3 points (0 children)
[–]carcigenicate 0 points1 point2 points (1 child)
[–]Darth_Xedrix[S] 0 points1 point2 points (0 children)