Python Python: How to show entered input results when entering a 0

Click For Summary
The discussion revolves around modifying a Python program to allow users to exit input loops and display results when a zero is entered. The current implementation successfully exits and displays results when a zero is entered for student names but fails to do so for grades. Suggestions include separating the loops for names and grades to prevent nesting issues and using an empty string or a negative number as exit signals instead of zero, which could conflict with valid grade entries. Clarification on the desired functionality is needed to ensure the program meets user expectations. The conversation emphasizes the importance of clear exit conditions for both loops in the program.
acurate
Messages
17
Reaction score
1

Homework Statement


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:

The Attempt at a Solution


Code:
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:
    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:
    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?
 
Technology news on Phys.org
acurate said:

Homework Statement


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:

The Attempt at a Solution


Code:
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:
    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:
    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?
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:
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:
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.
 
Mark44 said:
In your code, the loop for entering the grade is nested within the loop for entering the name. The two loops should be separate.
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.
 
DrClaude said:
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.
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
1
Views
2K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K