Python: how to break two times to results

  • Context: Python 
  • Thread starter Thread starter acurate
  • Start date Start date
  • Tags Tags
    Break Cycle Python
Click For Summary
SUMMARY

The discussion centers on a Python program designed to collect student names and grades, terminating input when a "0" is entered for either. The original code structure is overly complex, with nested loops for name and grade input. A simplified approach is recommended, utilizing separate loops for each input type. Additionally, the validation for grades should correctly handle the range from 0 to 10 without quotes around numeric comparisons.

PREREQUISITES
  • Understanding of Python programming fundamentals
  • Knowledge of input validation techniques in Python
  • Familiarity with lists and their manipulation in Python
  • Basic understanding of control flow using loops and conditionals
NEXT STEPS
  • Refactor the program to use separate loops for name and grade input
  • Implement input validation for grades to ensure they are numeric and within the specified range
  • Explore Python's exception handling to manage invalid inputs more gracefully
  • Learn about data structures in Python to optimize the storage of student names and grades
USEFUL FOR

Python developers, educators creating grading systems, and anyone interested in improving their input validation techniques in Python applications.

acurate
Messages
17
Reaction score
1

Homework Statement


I need a program, which would break and show results once a 0 is entered in input name and input grade.
I figured how to do that in name, but how do I add another break? Like grade !="0"?

Homework Equations

The Attempt at a Solution


Code:
students = []
grades = [] 

while True:
    name = input ("Enter a name: ")
    if  name.isalpha() == True and name != "0":
        while True:
            grade = input("Enter a grade: ")
            if grade.isdigit()== True:
                grade = int(grade)
                if grade >= 1 and grade <= 10:
                    if name in students:
                        index = students.index(name)
                        grades[index].append(grade)
                        break
                    else:
                        students.append(name)
                        grades.append([grade])
                        break
                else:
                    print("Grade is not valid. Try to enter it again!")
    elif name == "0":
        print("A zero is entered!")
        break 
    else:
        print ("Grade is not valid. Try to enter it again!")

for i in range(0,len(students)):
    print("NAME: ", students[i])
    print("GRADES: ", grades[i])
    print("AVERAGE: ", round(sum(grades[i])/len(grades[i]),1), "\n")
 
Technology news on Phys.org
acurate said:

Homework Statement


I need a program, which would break and show results once a 0 is entered in input name and input grade.
I figured how to do that in name, but how do I add another break? Like grade !="0"?

Homework Equations

The Attempt at a Solution


Code:
students = []
grades = []

while True:
    name = input ("Enter a name: ")
    if  name.isalpha() == True and name != "0":
        while True:
            grade = input("Enter a grade: ")
            if grade.isdigit()== True:
                grade = int(grade)
                if grade >= 1 and grade <= 10:
                    if name in students:
                        index = students.index(name)
                        grades[index].append(grade)
                        break
                    else:
                        students.append(name)
                        grades.append([grade])
                        break
                else:
                    print("Grade is not valid. Try to enter it again!")
    elif name == "0":
        print("A zero is entered!")
        break
    else:
        print ("Grade is not valid. Try to enter it again!")

for i in range(0,len(students)):
    print("NAME: ", students[i])
    print("GRADES: ", grades[i])
    print("AVERAGE: ", round(sum(grades[i])/len(grades[i]),1), "\n")
Your program structure is overly complicated, with your loop to read and validate the grade nested within your loop to read and validate the name. You can simplify your logic greatly by using two separate (unnested) loops, the first loop to get the name, and the second loop to get the grade.

Also, unlike name, which is a string of characters, grade should be a number. From your code, it appears that valid grades are between 0 and 10, so a typical if statement should not use quotes.
Python:
if grade >= 0 and grade <= 10:
    // Handle valid grade
    break
else:
     print("Grade is not valid. Please try again!")
 
Last edited:

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
2K
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K