Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

python 3.x - Class list - How would I make this repeat to the amount of students entered?

How would I make this repeat to the amount of students entered? Everything I've tried doesn't work.

numStudents = input("Enter the number of students: ")

while true:
    for x in range(len(numStudents)):
        name = input("Student Name: ")
        number = input("Student Number: ")

ex.

Enter the number of students: 3

Student name:
Student Number:

Student name:
Student Number:

Student name:
Student Number:

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. Get rid of the while true: (it's called True in Python, and there's no reason for an infinite loop even if you fixed that)
  2. Replace for x in range(len(numStudents)): with for x in range(int(numStudents)): to loop numStudents times, rather than looping "length of the string stored in numStudents" times. Or just change numStudents = input("Enter the number of students: ") to numStudents = int(input("Enter the number of students: ")) to make it an int from the get go, and make the for loop for x in range(numStudents):

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...