-1

I'm supposed to write a program with a loop that lets the user enter a series of integers, followed by -99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered.

This is what I have so far:

def main():
    user_input = 1
    while user_input != -99:
        user_input = int(input("Enter your number or -99 to end."))
        bigger = largest(user_input)
        tinier = smallest(user_input)
    print('The largest number is ', bigger, "and the smallest is ", tinier, ".")     

def largest(number):
    largest = 0
    if number > largest:
        largest = number
        return largest 

def smallest(number):
    smallest = 10000
    if number < smallest:
        smallest = number
        return smallest


main()

For some reason the sentinel value (-99) is entering the loop, I have no clue how, and becoming the smallest value. On top of that, the biggest value isn't ever the right one. Help much appreciated!

Cesco
  • 3
  • 1
  • 1
  • 2
  • 4
    Of course it is. You accept the input, check smaller and bigger, *then* the next iteration of the `while` loop is checked. – Cory Kramer Oct 17 '14 at 19:11
  • 1
    You are also *resetting* your `smallest` and `largest` variables each time you call the functions by the same name. Those local variables are cleared each time the function exits. – Martijn Pieters Oct 17 '14 at 19:12
  • On top of both of those problems, you're also only doing the `return largest` or `return smallest` if the new number is `>0` or `<10000`; otherwise you have no `return` statement, because it's indented under the `if`, so you will return `None`. Not that this matters very much until you fix the previous problem, but once you do you'll need to fix this too. – abarnert Oct 17 '14 at 19:19

4 Answers4

0

The quickest change to make to your code to fix this would be

def main():
    user_input = 1
    while user_input != -99:
        user_input = int(input("Enter your number or -99 to end."))
        if use_input == -99:
            break
        bigger = largest(user_input)
        tinier = smallest(user_input)
    print('The largest number is ', bigger, "and the smallest is ", tinier, ".")

The problem is, if the user enters -99, you complete the rest of the lines for that iteration of the loop. It will not terminate the while loop until the next time around, but it has already performed largest and smallest at that point so it is already overwritten.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

You have two problems as far as I can see: your input is being processed before being checked, and there are issues in your largest() and smallest() functions. When you scan for user input, you immediately go into your functions before verifying. Restructure your loop like this:

input()
while(){
  ...
  ...
  input()
}

For the second part, your functions aren't working because you initialize the values every time they run. Initialize your functions in the header at the top of your file, then just compare them. So for example, move the line largest=0 to the top of your file right below your import statements. Other than that, I think it should work.

Erki M.
  • 5,022
  • 1
  • 48
  • 74
DMo
  • 1
  • 1
0

Your Indentation is important in python so your smallest value and largest value functions return statement is improperly indent

def largest(number):
    largest = 0
    if number > largest:
        largest = number
    return largest 

def smallest(number):
    smallest = 10000
    if number < smallest:
        smallest = number
    return smallest
Danial Hussain
  • 2,488
  • 18
  • 38
0

Pretty simple if you use a list to store the numbers and rely on max/min functions from the standard library:

def main():
  numbers = []
  while True:
    user_input = int(raw_input("Enter a number"))
    if user_input == -99:
        break
    else:
        numbers.append(user_input)
  print('Largest is {}, smallest is {}'.format(max(numbers), min(numbers)))
Itxaka
  • 767
  • 6
  • 10