python - How do I get a function inside of a while loop in a function to work properly? -
i have looked , looked answer, new python , answers find seem on head or not quite need. trying take code , turn multiple functions complete simple task of receiving grades user , displaying input user want it. more clear when see 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 @ least 1 grade program work. bye." sys.exit() #def get_info(): while true: grades = raw_input("please input grades.(or enter once have entered of grades): ") if not grades: break elif validate_input() == true: continue else: print "please enter number." continue def give_answers(gradelist): while true: print "\n", print "please select grades." print "press 1 highest grade." print "press 2 lowest grade." print "press 3 average of grades entered." print "\n", print "or can press 'q' quit." choice = raw_input("> ") if choice == 'q': break elif choice == '1': print "\n", print "the highest grade entered %d.\n" % highest, elif choice == '2': print "\n", print "the lowest grade entered %d.\n" % lowest, elif choice == '3': print "\n", print "your average grade %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 have pasted code, when try define get_info
nested validate_input()
function stops working. no longer populates list exit()
catches , ends program. loop seems pass on validate_input()
function altogether because else statement trips after each time through. question how validate_input()
function work while continuing accept input until user presses enter?
pass grade function validate_input add grade list like:
def validate_input(grades):
Comments
Post a Comment