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

  • Context: Python 
  • Thread starter Thread starter acurate
  • Start date Start date
  • Tags Tags
    Input Loop Python
Click For Summary

Discussion Overview

The discussion revolves around a programming problem related to handling user input in a Python program. The goal is to modify the program so that entering a 0 in the grade input prompts the program to stop and display results, similar to how entering a 0 in the name input functions. The context includes coding practices and logic flow in loops.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their current implementation and the output when entering a 0 for the name input, seeking a similar result for the grade input.
  • Another participant suggests that the loops for entering names and grades should be separate to achieve the desired functionality.
  • Some participants express uncertainty about the original poster's (OP) intent, suggesting that the OP may want to loop over students and enter multiple grades for each, with a 0 breaking out of both loops.
  • There is a proposal that using an empty string to exit the student name input loop might be more appropriate than entering a 0.
  • Concerns are raised about using 0 as a sentinel for grades since a grade of 0 could be valid, with suggestions for using a negative number instead.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to modify the program. There are competing views on how to structure the loops and what sentinel values to use for exiting them.

Contextual Notes

Participants note that the current implementation has nested loops, which may not align with the OP's intended functionality. Clarification of the problem statement is suggested to better address the issue.

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.
 

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
3K
  • · Replies 2 ·
Replies
2
Views
2K