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
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
3 replies · 2K views
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?
 
Physics 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.