How to use one loop for two different lists

I want to print a sentence inside a for loop, where another iteration of the sentence is printed for each other situation, that is, I have two different lists: student_result_reading and student_name

student_result_reading = [] student_name = [] while True: student_name_enter = input("Please enter student name: ") student_name.append(student_name_enter) student_enter = int(input("Please enter student result between 0 - 100%: ")) student_result_reading.append(student_enter) continueask = input("Would you like to enter someone else? ") if continueask == "yes": continue else: break for studread, studentname in student_result_reading, student_name: print("Student Name: {} | Test Name: Reading Test | Percentage Score: {}".format(studentname, studread)) 

Here are my two problems:

  • When I enter 2 or more names, they are not formatted correctly.

  • When I enter 1 name, I get an error.

Any help on any decisions is appreciated.

+5
source share
7 answers

You can use the built-in zip function to do this:

 for studread, studentname in zip(student_result_reading, student_name): print("Student Name: {} | Test Name: Reading Test | Percentage Score: {}".format(studentname, studread)) 

Also, if you are using Python 2, you are probably facing a problem using the following two lines:

 student_name_enter = input("Please enter student name: ") 

and

 continueask = input("Would you like to enter someone else? ") 

Ie, if you enter something like student name as the input for the student name, you will get SyntaxError or NameError . Of course, in Python 2, the input function expects a valid Python expression, in your case a string such as "student name" , not just student name . For a later expression that must be valid, you can use the raw_input function.

+3
source

I prefer to access array elements by index, for example:

 for x in range(len(student_name)): print("Student Name: {} | Test Name: Reading Test | Percentage Score: {}".format(student_name[x], student_result_reading[x])) 
0
source

I am not very versed in python, but what can you do, I keep a counter for the number of students, say count_students

 For students in range(0,count_students): print(student_name[student] + " ") print(student_result[student] + "\n") 
0
source

The code will only work if you enter exactly two students. The main problem is not understanding the for loop at the end.

 for x, y in [1,2],[4,3]: print(x,y) 

Will be printed

 1,2 4,3 

He will not print

 1,4 2,3 

as you would like. This explains why you see incorrect formatting when you enter exactly two students. You will want to use zip to join the two lists, as indicated in another answer.

 zip([1,2],[4,3]) will equal [(1,4),(2,3)] 

so your for loop will work as intended.

0
source

An error is generated using this: "enter"

i reeplaced by "raw_input".

you can paste and run this code.

 student_result_reading = [] student_name = [] while True: # student_name_enter = input("Please enter student name: ") student_name_enter = raw_input("Please enter student name: ") student_name.append(student_name_enter) # student_enter = int(input("Please enter student result between 0 - 100%: ")) student_enter = int(raw_input("Please enter student result between 0 - 100%: ")) student_result_reading.append(student_enter) # continueask = input("Would you like to enter someone else? ") continueask = raw_input("Would you like to enter someone else? ") if continueask == "yes": continue else: break # for studread, studentname in student_result_reading, student_name: # print("Student Name: {} | Test Name: Reading Test | Percentage Score: {}".format(studentname, studread)) for studread, studentname in zip(student_result_reading, student_name): print("Student Name: {} | Test Name: Reading Test | Percentage Score: {}".format(studentname, studread)) 
0
source
 student_result_reading = {} #Create a dictionary to store the students name, and their score while True: student_name_enter = input('Please enter a student name: ') #Ask the user for a student name student_enter = int(input('Please enter a student result between 0 - 100%: ')) #Have the user enter an integer for the student score student_result_reading[str(student_name_enter)] = student_enter #Make the key of the dict element the students name, value the students score continue_ask = input('Please enter yes to continue, or enter (q) to quit: ') #Prompt user to enter q to quit #If prompt == q, break out of the loop if continue_ask == 'q': break for student, score in student_result_reading.items(): #Use the .items() method of the dict to get the key, and corresponding value for each element in list print('Student name: {0} | Test Name: Reading Test | Percentage Score: {1}'.format(student, score)) 

To simplify this, you can add the student name in the dictionary instead of using two lists. We are still starting the while loop and requesting user input for the student name and student grade. Then change the dictionary by adding a key, which will be the name of the student, and set the value equal to the amount of students. Then we can ask the user to continue or exit the cycle by entering q. We can use the .items () method of the dictionary to iterate over each key and its corresponding value. Then we get the result you are looking for in the same dictionary, and not in two separate lists, with student names being keys and student ratings being values. Here is the result:

  Please enter a student name: Random Student Please enter a student result between 0 - 100%: 90 Please enter another student name and score, or enter (q) to quit: yes Please enter a student name: Random Student2 Please enter a student result between 0 - 100%: 75 Please enter another student name and score, or enter (q) to quit: q Student name: Random Student | Test Name: Reading Test | Percentage Score: 90 Student name: Random Student2 | Test Name: Reading Test | Percentage Score: 75 
0
source

Let me explain this a lot easier:

 pets = ['cat', 'dog'] ages = [5, 7] for pet, age in pets, ages: print(pet, age) 

What is printed here:

 cat dog 5 7 

The crucial question is what is really going on here. It turns out that pets, ages is actually a tuple. Expressions separated by commas are called expression-lists .

This is a tuple containing 2 lists, it looks like this: (['cat', 'dog'], [5, 7]) . Therefore, when you iterate over it, the following interesting thing arises: Iterative unpacking!

Basically what is happening now:

 pet, age = ['cat', 'dog'] 

And at the next iteration:

 pet, age = [5, 7] 

How did you get an initially amazing result.

The reason for the second problem that you encountered is that you supply only one name, at the first iteration it happened (let the pets example still be used):

 pet, age = ['python'] 

ValueError: need more than 1 value to unpack

To fix both problems, you can use the built-in zip function .

 for studentname, studread in zip(student_name, student_result_reading): print('Student Name: {} | Test Name: Reading Test | Percentage Score: {}'.format(studentname, studread)) 

You can also use the so-called f-strings to format the output, they are available with Python 3.6

 for studentname, studread in zip(student_name, student_result_reading): print(f'Student Name: {studentname} | Test Name: Reading Test | Percentage Score: {studread}') 
0
source

Source: https://habr.com/ru/post/1276259/


All Articles