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

In summary, the user is trying to create a program that will allow them to enter multiple students' names and grades, with the ability to exit the program by entering an empty string for the student name or a negative number for the grade. The current issue is that the grades loop is nested within the names loop, causing difficulty in breaking out of both loops simultaneously. A better solution would be to have the two loops separate and use different sentinels for exiting the program.
  • #1
acurate
17
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 

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

1. What is the purpose of using "Python" to show entered input results when entering a 0?

The purpose of using Python to show entered input results when entering a 0 is to create a program or script that allows users to input data and receive immediate feedback on their input. This is especially useful in situations where the user needs to continuously input data and see results in real-time.

2. How can I use "Python" to show entered input results when entering a 0?

To use Python to show entered input results when entering a 0, you can create a loop that continuously prompts the user for input and checks if the input is equal to 0. If the input is equal to 0, the loop can break and the result can be displayed. Alternatively, you can use the input() function to receive user input and use an if statement to check if the input is equal to 0 before displaying the result.

3. Is "Python" the only programming language that can show entered input results when entering a 0?

No, there are other programming languages that can also achieve this functionality, such as Java, C++, and JavaScript. It ultimately depends on the specific needs and preferences of the user.

4. Can I customize the output of "Python" when showing entered input results?

Yes, you can customize the output of Python when showing entered input results by using string formatting techniques or by using conditional statements to display different outputs based on the input. You can also use functions to manipulate the input and output in a desired format.

5. Are there any potential errors or issues to be aware of when using "Python" to show entered input results when entering a 0?

One potential issue to be aware of is that the user may enter an invalid input that is not equal to 0, which can cause the program to not produce the desired result. It is important to handle these types of errors by adding proper validation and error-handling techniques to the program. Additionally, depending on the complexity of the program, there may be other potential errors or issues that could arise, so it is always important to thoroughly test and debug the code.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Replies
17
Views
8K
Back
Top