Home > database >  About the simple problem in Python (Array)
About the simple problem in Python (Array)

Time:09-26

I am going to make a full chart into Python, like below. The score itself will be provided from the user by using int(input()).

Subject A B C D E

Sociology 89 78 98 78 45

Algebra 78 98 65 78 65

English 45 98 45 61 37

into midterm_score = [[89, 78, 45], [78, 98, 98], [98, 65, 45],[78, 78, 61], [45, 65, 37]]

My code is

midterm_score = [0, 0, 0, 0, 0]
student = [0,0,0]
for i in range (0, 4):
    print("Enter Student "   str(i 1)   "'s Sociology Score: ")
    Soc_score = int(input())
    print("Enter Student "   str(i 1)   "'s Algebra Score: ")
    Alg_score = int(input())
    print("Enter Student "   str(i 1) "'s English Score: ")
    Eng_score = int(input())
    student[i] = [Kor_score, Math_score, Eng_score]
midterm_score = [student[0], student[1], student[2], student[3], student[4]]
print(midterm_score)

then, I got the error code

Traceback (most recent call last):
  File: "C:/Users/User/PycharmProjects/pythonProject/main.py", line 10, in <module>
    student[i] = [Soc_score, Alg_score, Eng_score]
IndexError: list assignment index out of range

so I tried extending the index range like

student = [0,0,0,0]

and

for i in range (0,5)

but still makes the same error code again and again. I would appreciate it if you could teach me well I am a first-time learner of this language.

CodePudding user response:

In python, the range function returns a sequence of numbers in a determined range. Using range with 2 parameters, determines the start and the end of the sequence.

Example:

nums = range(10, 16)
print(nums)
# output: [10, 11, 12, 13, 14, 15]

Notice that 10 (the start of the range) was present, but 16 (the end of the range) was not.

Therefore in your code, when students is of length of 3, you will need to have the range return an indexes list of 3 elements, between 0 and 3, instead of 0 and 4. This will give you [0, 1, 2] - the indexes you need. This is because in Python, like most languages, indexes of sequences starts with 0, so the largest index of a list with n elements, is n-1

CodePudding user response:

Python range is inclusive-exclusive, consider that

print(list(range(0,5)))

output

[0, 1, 2, 3, 4]

Thus if you want to access indices of list using range(0,5) it needs to have at least 5 elements i.e. student = [0,0,0,0,0] not student = [0,0,0,0]. As side note if first argument you give to range is 0 you might left it, that is:

for i in range(0,3):
    print(i)

is equivalent to

for i in range(3):
    print(i)

CodePudding user response:

Never mind me, just shrinked your code in here

And also try it and tell me if this is what you want...

student = [
    [
     int(input(f"Enter Student {str(i 1)}'s Sociology Score: ")),
     int(input(f"Enter Student {str(i 1)}'s Algebra Score: ")),
     int(input(f"Enter Student {str(i 1)}'s English Score: "))
    ] for i in range(4)] 
    # Gets user input 4 times and appended to student variable

midterm_score = [*student[:4]] # Unpacks list
print(midterm_score)

tell me if its okay for you...

  • Related