Python: how to break two times to results

  • Context: Python 
  • Thread starter Thread starter acurate
  • Start date Start date
  • Tags Tags
    Break Cycle Python
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
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")
 
Physics 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: