Skip to main content
  1. Teaching/
  2. teaching/

·3 mins

Minimax

Write a program which accepts an arbitrary number of integers as input from the user and prints the minimum and maximum of numbers entered. The user will stop the input loop by typing in the string "stop". Once the user enters "stop", you'll print the minimum and maximum of the numbers entered. See the examples below for an example of how the program should work.

> python3 MinMax.py
Enter an integer or 'stop' to end: stop
You didn't enter any numbers
> python3 MinMax.py
Enter an integer or 'stop' to end: 99
The maximum is 99
The minimum is 99
> python3 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: 27
Enter an integer or 'stop' to end: +35
Enter an integer or 'stop' to end: -100
Enter an integer or 'stop' to end: stop
The maximum is 35
The minimum is -100

Note that we haven't covered lists in class yet, so don't use them. You don't need them for this assignment anyways.

Tips

Here are some tips to remember while you write this assignment:

  • All input arrives as strings, so you need to check if you have the "stop" string before you convert it to an integer.
  • You can assume that everything entered by the user is either an integer or the string "stop" (in all lowercase). You don't have to check if this is true or not.
  • Usually, you write a for-loop when you know how many times a loop will run, and a while-loop otherwise. Which situation are we in for this problem?
  • Sometimes, it makes sense to use True as a condition for a while-loop. Of course, you run the risk of infinite loops if you don't have a way to get out of the loop in this case. You have to make sure to test inside the loop body and use break to get out of the loop when you need to.

Insight

There's not much terribly interesting about this particular program. However, think about the error handling aspect of this program: we told you not to worry about the inputs. What kind of nasty inputs could a user feed to your program? How do you think your program should respond if it gets an invalid input:

    a
  • Should it stop or should it keep processing input?
  • When the error occurs, should it print the results so far?
  • When the error occurs, should it print an error message for the user?

Write a one-to-two sentence justification of your response for each.

Submission

Submit a single file named MinMax.py (pay attention to capitalization!) on Canvas. Your file needs to compile and run. It should also have a header with the following information (this goes in your source file, not in the program output):

# File: MinMax.py
# Student: 
# Course: Intro to Programming
# 
# Date:
# Description of Program:
# What kinds of bad input could a user give to your program?
# How should your program respond to bad input? (Answer the 3 questions from "Insight")

The description should be a short (1-3 sentence) description of what the program does. Do not describe how it's written! (Think back to HW 0 to see the difference). Also include the three test cases you ran on the program and their outputs. If the outputs seem abnormal, comment on why you think they might be wrong.