Python Python: how to break two times to results

AI Thread Summary
The discussion focuses on creating a program that collects student names and grades, terminating when a zero is entered for either input. The original code successfully handles the name input but struggles to implement a similar break condition for grades. It uses nested loops for input validation, which complicates the structure. Suggestions include simplifying the logic by using separate loops for names and grades, and ensuring that grade validation checks for numeric input without quotes. The valid grade range should be clarified to include zero, and the program should handle invalid inputs more effectively. Overall, the emphasis is on improving code clarity and functionality.
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top