CS303E Homework 5

Instructor: Dr. Bill Young
Due Date: Friday, September 22, 2023 at 11:59pm

Assignment

Write a program in file MinMax.py that accepts an arbitrary number of integer inputs from the user and prints out the count of numbers entered, the minimum and maximum of the numbers entered. The user will end the input loop by typing in the string 'stop'. When the user enters 'stop' your program should print out a blank line, the count of numbers entered, the minimum integer entered and the maximum integer entered. If the user enters 'stop' immediately (i.e., before any numbers are entered), you'll print a slightly different message. See the examples below.

You can assume that the values entered are integers (positive, negative or zero) or the string 'stop' (lowercase). You don't have to validate the inputs. Everything is separated by a single space.

Remember that all input arrives as strings. So you have to check whether it's the string 'stop' before you convert it to an integer.

Below is some sample output. You should mimic this exactly for the given inputs.

> python MinMax.py
Enter an integer or 'stop' to end: stop

You didn't enter any numbers
> python MinMax.py
Enter an integer or 'stop' to end: 87
Enter an integer or 'stop' to end: stop

You entered 1 number
The maximum is 87
The minimum is 87
> python MinMax.py
Enter an integer or 'stop' to end: -10
Enter an integer or 'stop' to end: 0
Enter an integer or 'stop' to end: +42
Enter an integer or 'stop' to end: 87
Enter an integer or 'stop' to end: -100
Enter an integer or 'stop' to end: stop

You entered 5 numbers
The maximum is 87
The minimum is -100
>
This homework is primarily designed to give you some practice using loops, but you'll also need some if statements.

Notice that conceptually you're accepting two different "types" of values from the user: numbers and the string 'stop'. In actuality, you're only accepting one type, since everything entered is a string. Your tendency might be to immediately coerce your inputs to integers using the int() function; but that's going to crash your program when the user enters 'stop'. Instead, take each string input (they're all strings) and first ask if it's 'stop'. If it is, handle that case. If not, only then apply int(). Think about the logic of this.

We haven't yet covered lists in the class, so don't use them. But you don't need them. Think about this problem as follows. Suppose we're talking and I tell you I'm going to rattle off a bunch of numbers. At the end, I want you to tell me the biggest number I mentioned. I start: "9, 15, 130, 2, 14, ...." Notice that you certainly don't need to remember all of the numbers. At any point, you only need to remember the biggest number I've mentioned to that point. If you hear a smaller number, you ignore it. But if you hear a bigger number, then that's the new biggest number so far and you just need to remember that one.

Notice that, if only one number is entered, my program prints out "You entered 1 number", not "You entered 1 numbers", which wouldn't be very good grammar. This is a simple fix, requiring an if statement. Make sure that you make this fix.

Turning in the Assignment:

The program should be in a file named MinMax.py. Submit the file via Canvas before the deadline shown at the top of this page. Submit it to the assignment weekly-hw5 under the assignments sections by uploading your python file.

Your file must compile and run before submission. It must also contain a header with the following format:

# File: MinMax.py
# Student: 
# UT EID:
# Course Name: CS303E
# 
# Date:
# Description of Program: 

Programming tips

Program incrementally: Don't try to write this entire program all at once. In the first pass, you might just handle the input---read numbers and print them until you encounter 'stop'. This will ensure you can handle looping while reading in both integers and strings and that you know when to stop. (But be sure you're treating the integers as numbers, and not as strings.) Then figure out to add in finding the minimum number. Then add finding the max---that's easy once you've done min. Finally add the counting. Each step should get easier since you've already confronted harder problems. You'll be done before you know it. Some of you will think: "I don't want to do all those steps; it's easier to just do it all at once." You're wrong!

Using Loops: You usually write a for loop if you know in advance how many times the loop will run and a while loop if you don't. For this problem, you don't know how many times the loop will run (iterations). So you have to figure out the loop test. The trick is to make sure that the loop test makes sense the first time you enter the loop.

For this problem, it seems to make sense to stop when the user input is "stop". But that means you must have received an input before you entered the loop. But then you don't want to have an input statement as the first thing in your loop, or you'll be throwing away that first input. You have to process that input before you read another one. Think about where you should put your input statement inside the loop.

Sometimes, it makes sense to write a loop test of True when you don't know how many iterations there will be. But then you run the risk of an infinite loop. You have to test inside the loop body to see if you are ready to exit and break if so. If not, you can always continue to another iteration of the loop. Years ago, I worked on a language called "Gypsy" in which all loops were like this. The loop statement had no test and the only way to exit a loop was an explicit break statement. That's probably why I often write loops that way.