1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python: how to break two times to results

  1. Mar 24, 2015 #1
    1. The problem statement, all variables and given/known data
    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"?
    2. Relevant equations

    3. The attempt at a solution
    Code (Text):

    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)
                        print("Grade is not valid. Try to enter it again!")
        elif name == "0":
            print("A zero is entered!")
            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")
  2. jcsd
  3. Mar 24, 2015 #2


    Staff: Mentor

    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.
    Code (Python):
    if grade >= 0 and grade <= 10:
        // Handle valid grade
         print("Grade is not valid. Please try again!")
    Last edited: Mar 24, 2015
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted