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.
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:
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.