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.
Stopping early: Often you'll want to exit your program early. For example, in this assignment if you find that the user enters 'stop' immediately, you'll print a message and be done. You can always do that with if statements, but it sometimes makes for a more complicated overall structure. Another way is to put your code into a function, main(). To exit main, you execute a return statement. Note you can't do this unless your code is inside a function. Here's what this might look like:
if inputString == 'stop': print("\nYou didn't enter any numbers.\n") returnThis is yet another reason to put your code into a main() function. BTW: your function doesn't have to be called main; we'll introduce user-defined functions in the next module.
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.
if-elif-else versus consecutive if: Some of you have wondered
whether to use an if-elif-else statement or just a series of if
statements. Sometimes, they do the same thing but often they don't
and one may be significantly more efficient. Suppose you have a
Cartesian coordinate system and you want to know what quadrant a
particular point (x, y) is in. You could do the following:
if x == 0 or y == 0: print("Point is on an axis") if x > 0 and y > 0: print("Point is in Quadrant I") if x < 0 and y > 0: print("Point is in Quadrant II") if x < 0 and y < 0: print("Point is in Quadrant III") if x > 0 and y < 0: print("Point is in Quadrant IV")This works because the conditions are mutually exclusive, i.e., it's impossible for any two conditions to both be True for any point. The problem is that you have to evaluate every one of the five conditions, even if you find, say, after the first test that the point is on an axis. This is quite inefficient. Instead, you might do the following:
if x == 0 or y == 0: print("Point is on an axis") elif x > 0 and y > 0: print("Point is in Quadrant I") elif y > 0: print("Point is in Quadrant II") elif x < 0: print("Point is in Quadrant III") else: print("Point is in Quadrant IV")Think about why this works; in particular, think about what must be true as you're evaluating any particular condition. This version is much more efficient because you only have to evaluate conditions until one is true. And also notice that you can't just replace the elifs by ifs in this version because then multiple tests could be true simulataneously and you'd get the wrong answer.