Acu
Acu

Reputation: 85

How to show the results of a program by entering something in input?

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:

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:

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:

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?

Upvotes: 5

Views: 1724

Answers (2)

You should look into functions as they would save you a lot of hassle here, however here is a commented version of your code that will exit on either input being 0 and display the end message.

import collections
students = collections.defaultdict(list)

while True: #combined while loops
    print ("Enter 0 for name or grade to exit") #you don't say how to exit
    student = input("Enter a name: ").replace(" ","")
    if student == "0":
        print ("A zero has been entered(0)")
        break
    grade = input("Enter a grade: ").replace(" ","")
    if grade == "0":
        print ("A zero has been entered(0)")
        break
    #can combined if student == 0 or grade == 0, but then both inputs must display

    if not student.isalpha() or not grade.isdigit():
        print ("You've entered an invalid name or grade. Try again.")
        #since these were the same message just combine them
        #can switch to too if statements if you want seperate messages
        #Also you might want to add a enter only alpha or digits for... message
        break

    grade = int(grade) 
    if 1 <= grade <= 10:
        students[student].append(grade)

for i, j in students.items():
    print ("NAME: ", i)
    print ("LIST OF GRADES: ", j)
    print ("AVERAGE: ", round(sum(j)/len(j),1))

Upvotes: 0

Zizouz212
Zizouz212

Reputation: 4998

In one of your inputs, check to see if the input is 0. If it is, make a call to sys.exit. This can be done anywhere, even when checking for the grade. Just make sure that you include both options then (a string 0 and an int 0). Also, the reason for why it was still saying A zero has been entered(0) is because you are only quitting out of one loop, and then continuing from the beginning basically.

# Be sure to import sys!

random_place = input("Whatever is here ")
if random_place == '0' or random_pace == 0:
    sys.exit(0)

Your program will exit then.

Upvotes: 1

Related Questions