Python: how to break two times to results

In summary, the student's name and grade must both be entered into the program in order to break and show results.
  • #1
acurate
17
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
  • #2
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:

Related to Python: how to break two times to results

1. What is the purpose of breaking two times in Python?

The purpose of breaking two times in Python is to exit out of multiple nested loops or conditional statements at once. This can save time and make code more efficient.

2. How do you break two times in Python?

To break two times in Python, you can use the "break" statement twice within the nested loops or conditional statements. This will break out of both loops or statements at the same time.

3. Can you break out of more than two nested loops or statements?

Yes, you can break out of multiple nested loops or statements by using the "break" statement multiple times. However, it is important to consider if breaking out of multiple loops is the best solution for your specific code.

4. What is the difference between "break" and "continue" in Python?

The "break" statement completely exits out of a loop or conditional statement, while the "continue" statement only skips the current iteration and continues onto the next one. "Break" is used to completely stop the loop or statement, while "continue" is used to skip certain iterations.

5. Are there any alternatives to using "break" to exit out of loops in Python?

Yes, there are alternative methods for exiting out of loops in Python, such as using the "return" statement or setting a condition to break out of the loop. However, the "break" statement is the most commonly used and recommended method.

Similar threads

  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
Replies
5
Views
928
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
22
Views
968
  • Programming and Computer Science
Replies
34
Views
3K
Back
Top