This is a question related to the Python list enumerate repeat statement.

Asked 2 years ago, Updated 2 years ago, 81 views

I'm writing code to make it like this, but it got stuck while writing no1~5 below.

score = [88,95,70,100,99]                                                                                                                                     
classes = ["Korean", "Math", "Science", "Physics", "Chemistry"]                                                    
for i, value in enumerate(score, 1):                                                                                                                  
      print("No{}: {}Score-{}".format(i, classes, value))

The code was written as above

No1: ['Korean', 'Mathematics', 'Science', 'Physics', 'Chemistry'] Score -88                                                                                           
No.2: ["Korean", "Mathematics", "Science", "Physics", "Chemistry"] Score - 95                                                                                 
No.3: ["Korean", "Mathematics", "Science", "Physics", "Chemistry"] Score - 70                                                                                 
No 4: [Korean, Math, Science, Physics, Chemistry] Score - 100                                                                              
No 5: ["Korean", "Math", "Science", "Physics", "Chemistry"] Score - 99

The above results were derived. If you know a solution, please answer.

python loops

2022-09-20 22:13

2 Answers

# in print
classes      # X
classes[i-1] # O


2022-09-20 22:13

You can also use zip together.

score = [88,95,70,100,99]                                                                                                                                     
classes = ["Korean", "Math", "Science", "Physics", "Chemistry"]                                                    
for i, (v, c) in enumerate(zip(score, classes), 1):
      print("No{}: {}Score-{}".format(i, c, v))


2022-09-20 22:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.