1. Not finding help here? Sign up for a free 30min 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 show entered input results when entering a 0

Tags:
  1. Mar 26, 2015 #1
    1. The problem statement, all variables and given/known data
    So, I have a program, which I am finishing but I apparently ran into a problem. In the program I have to make that whenever I enter a 0 in the input, the program will stop and print the results. Here's the program before I continue saying anything else:

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

    import collections
        students = collections.defaultdict(list)
       
        while True:
            student = input("Enter a name: ").replace(" ","")
            if student == "0":
                print ("A zero has been entered(0)")
                break
       
            if not student.isalpha():
                    print ("You've entered an invalid name. Try again.")
                    continue
       
            while True:
                grade = input("Enter a grade: ").replace(" ","")
                if grade == "0":
                    print ("A zero has been entered(0)")
                    break
               
                if not grade.isdigit():
                    print ("You've entered an invalid name. Try again.")
                    continue
               
                grade = int(grade)
                if 1 <= grade <= 10:
                    students[student].append(grade)
                    break
       
        for i, j in students.items():
            print ("NAME: ", i)
            print ("LIST OF GRADES: ", j)
            print ("AVERAGE: ", round(sum(j)/len(j),1))
     
    I figured out a way how to make the program stop and post results after a 0 is entered in the `"Enter a name: "` part. This is what is printed out:

    Code (Text):

        Enter a name: Stacy
        Enter a grade: 8
        Enter a name: 0
        A zero has been entered(0)
        NAME:  Stacy
        LIST OF GRADES:  [8]
        AVERAGE:  8.0
    I need to do the same with the `"Enter a grade: "` part but if I try to make the program like it is now, this is what it prints out:

    Code (Text):

        Enter a name: Stacy
        Enter a grade: 0
        A zero has been entered(0)
        Enter a name:
    How do I make the program show results like it does when a 0 is entered in the name input?
     
  2. jcsd
  3. Mar 26, 2015 #2

    Mark44

    Staff: Mentor

    In your code, the loop for entering the grade is nested within the loop for entering the name. The two loops should be separate. Here's what you have:
    Code (Python):

        while True:
            student = input("Enter a name: ").replace(" ","")
            if student == "0":
                print ("A zero has been entered(0)")
                break
     
            if not student.isalpha():
                    print ("You've entered an invalid name. Try again.")
                    continue
     
            while True:
                grade = input("Enter a grade: ").replace(" ","")
                if grade == "0":
                    print ("A zero has been entered(0)")
                    break
    Here's how it should look:
    Code (Python):

        while True:
            student = input("Enter a name: ").replace(" ","")
            if student == "0":
                print ("A zero has been entered(0)")
                break
     
            if not student.isalpha():
                    print ("You've entered an invalid name. Try again.")
                    continue
     
       while True:
          grade = input("Enter a grade: ").replace(" ","")
              if grade == "0":
                 print ("A zero has been entered(0)")
                    break
                    // etc.
    Notice that in the latter version, the two while statements have the same indentation level. In your code, the second while is indented relative to the first while, which makes the second while loop nested within the first one. You don't want that.
     
  4. Mar 26, 2015 #3

    DrClaude

    User Avatar

    Staff: Mentor

    I don't think that this is what the OP wants. I think that what they want is to loop over students, and then enter many grades for each student. They then want a zero to break out of both loops. If that is correct, then there is the problem of how to break the inner loop only, when all the grades of one student are entered, to move on to the next student.

    I'll wait until the OP has clarified the task to be accomplished before replying further.
     
  5. Mar 26, 2015 #4

    Mark44

    Staff: Mentor

    Yes. A complete statement of the problem would be helpful.

    Also, rather than entering 0 for the student name to exit the loop, a better solution might be if the user enters an empty string. And to exit the grades loop, 0 is not a good sentinel, as a grade of 0 could be a valid grade. Entering a negative number could serve as the test to exit the grades loop.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Python: How to show entered input results when entering a 0
  1. Steam entering turbine (Replies: 15)

Loading...