1

I have looked and looked for an answer, but I am new to python and the answers I find seem to be over my head or not quite what I need. I am trying to take my code and turn it into multiple functions to complete the simple task of receiving some grades from the user and displaying the input back to the user as they want it. Hopefully this will be more clear when you see my code.

import sys

gradeList = []

def validate_input():
    try:
        gradeList.append(int(grades))
    except:
        return False
    else:
        return True

def average(count, total):
    ave = total / count
    return ave

def exit(gradeList):
    if gradeList == []:
        print "You must enter at least one grade for this program to work. Good Bye."
        sys.exit()

#def get_info():
while True:
    grades = raw_input("Please input your grades.(or Enter once you have entered all of your grades): ")
    if not grades:
        break
    elif validate_input() == True:
        continue
    else:
        print "Please enter a number."
        continue

def give_answers(gradeList):    
    while True:
        print "\n",
        print "Please select what you would like to do with your grades."
        print "Press 1 for the Highest grade."
        print "Press 2 for the Lowest grade."
        print "Press 3 for the Average of the grades you entered."
        print "\n",
        print "Or you can press 'q' to quit."
        choice = raw_input("> ")
        if choice == 'q':
            break
        elif choice == '1':
            print "\n",
            print "The highest grade that you entered was %d.\n" % highest,
        elif choice == '2':
            print "\n",
            print "The lowest grade that you entered was %d.\n" % lowest,
        elif choice == '3':
            print "\n",
            print "Your average grade was %d.\n" % average,
        else:
            print "Please enter 1, 2, 3 or 'q'."


#get_info()
exit(gradeList)

gradeList.sort()
highest = gradeList[-1]
lowest = gradeList[0]
count = len(gradeList)
total = sum(gradeList)
average =  average( count, total)

give_answers(gradeList)

Everything works correctly as I have pasted the code, but when I try to define get_info the nested validate_input() function stops working. It no longer populates the list so exit() catches and ends the program. The loop seems to just pass over the validate_input()function altogether because the else statement trips after each time through. My question is how do I get the validate_input() function to work properly while continuing to accept input until the user presses enter?

  • You should provide explicit parameters and return values for your functions, rather than rely on scoping. Also, see: http://stackoverflow.com/q/23294658/3001761 – jonrsharpe Dec 16 '14 at 10:49
  • I read through the link, Great Stuff! What do you mean about the explicit parameters and return values? SMH I'm such a newb! – TJ_Lightning Dec 16 '14 at 11:38
  • https://docs.python.org/2/tutorial/controlflow.html#defining-functions – jonrsharpe Dec 16 '14 at 11:39

1 Answers1

0

Pass grade to your function validate_input so it could add the grade to list like:

def validate_input(grades):
SMA
  • 36,381
  • 8
  • 49
  • 73