i have an introduction to programming class and i'm trying to write this program and the program is supposed to loop if you don't input a number within a specific range but it doesnt do it

def main():
    endProgram = 'no'
    print
    while endProgram == 'no':
        totalScores = 0
        averageScores = 0
        number = 0
        number = getNumber(number)
        totalScores = getScores(totalScores, number)
        averageScores = getAverage(totalScores, averageScores, number)
        printAverage(averageScores)

        endProgram = raw_input('Do you want to end program? (Enter yes or no): ')
        while not(endProgram == 'yes' or endProgram == 'no'):
            print'Please enter a yes or no'
            endProgram = raw_input('Do you want to end program? (Enter no to process a new set of scores): ')

        

                
#this function will determine how many students took the test
def getNumber(number):
    
    number = input('Enter a number between 2 and 30: ')
    while number >=2 or number <=30:
        print'You must enter a number between 2 and 30'
        number = input('Enter a number between 2 and 30:')
        
    return number    

#this function will get the total scores
def getScores(totalScores, number):
    for counter in range(0, number):
        score = input('Enter their score: ')
        totalScores = totalScores + score
    return totalScores

#this function will calculate the average
def getAverage(totalScores, averageScores, number):
    averageScores = totalScores / number
    return averageScores

#this function will display the average
def printAverage(averageScores):
    print 'The average test score is', averageScores  
    
# calls main
main()

Recommended Answers

All 4 Replies

line 25 should be while number < 2 or number > 30.

By the way, help is a very poor thread title. Try to be more descriptive of the thread's content.

line 25 should be while number < 2 or number > 30.

By the way, help is a very poor thread title. Try to be more descriptive of the thread's content.

thanks for the help, i appreciate it

thanks for the help, i appreciate it

Here is a general function to get a bounded value from the user

def get_user_value(min=None, max=None, message=None, converter=float):
    if (min is not None) and (max is not None) and (max < min):
        raise ValueError("Impossible min and max values")
    if message is None:
        message = "Please enter a value"
        if min is None:
            if max is not None:
                message += " <= {max}".format(max = str(max))
        elif max is None:
            message += " >= {min}".format(min = str(min))
        else:
            message += " between {min} and {max}".format(min = min, max = max)
    while True:
        user_string = raw_input(message + ": ")
        error = False
        try:
            value = converter(user_string)
        except ValueError:
            error = True
        error = error or ((min is not None) and (value < min)) or ((max is not None) and (value > max))
        if not error:
            return value
        print "Invalid value"
        
        
if __name__ == "__main__":
    val = get_user_value(min=1, max=2)

Would be maybe cleaner not to use function names min and max, even it is this local scope as keyword parameters. Matter of style only, has points of easy to remember keyword name.

Here little modified version:

def get_user_value(start=None, end=None, message=None, converter=float):
    if (start is not None) and (end is not None) and (end < start):
        raise ValueError("Impossible start and end values")
    message = "Please enter a value" if message is None else message
    if all(val is not None for val in (start,end)):
        message += ' between {start} and {end}'.format(start = start, end= end)
    else:
        message += " <= {end}".format(end = str(end)) if end is not None else ''
        message += " >= {start}".format(start = str(start)) if start is not None else ''
    while True:
        user_string = raw_input(message + ": ")
        try:
            value = converter(user_string)
            if ((start is not None) and (value < start)) or ((end is not None) and (value > end)):
                raise ValueError("Invalid value")
        except ValueError as e:
            print e
        else:
            return value
 
if __name__ == "__main__":
    val = get_user_value(start=0, end=2)
    print 'Value is %f' % val
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.