成绩计算异常(仅首名学生正确)及循环提示序号优化咨询
Hey Jessica, let's work through those two coding issues you've run into. I'll break down clear solutions for each one:
The core problem here is that your assessment and weight lists are defined outside the main function. That means after processing the first student, these lists still hold their old data—so when you calculate the second student's grade, it's using the first student's assessment details instead of fresh inputs.
To fix this, move the list initializations inside the main function. This way, every time you run main() for a new student, it starts with empty, clean lists. Here's the adjusted code snippet:
def main(): # Initialize empty lists inside the function to reset for each student assessment = [] weight = [] assign_number = int(input('How many assessments: ')) for i in range(assign_number): asses_name = input(f'Enter name of assessment {i+1}: ') assessment.append(asses_name) # Add weight input logic here (example included) asses_weight = float(input(f'Enter weight for assessment {i+1}: ')) weight.append(asses_weight) # Insert your grade calculation logic here # ... # Call main() once per student main() main()
You can use Python's f-strings to dynamically insert the assessment number into the input prompt. Since range(assign_number) starts counting from 0, just add 1 to the loop variable i to get human-readable numbers (1, 2, 3...).
Replace your original input line with this:
asses_name = input(f'Enter name of assessment {i+1}: ')
Now the loop will generate prompts exactly like Enter name of assessment 1: for the first iteration, Enter name of assessment 2: for the second, and so on.
内容的提问来源于stack exchange,提问作者Jessica

