Uploaded by capogia57

2401-W24-ICE3-TryCatch

advertisement
BUSI 2401
In Class Exercise 3: Looping, If and Try Catch
NOTE: Use of ChatGPT or other AI is strictly prohibited. Use of such tools will be treated as an
academic integrity offense.
Write a function named getNumber() with the following requirements:
- It accepts three arguments
o a string that is a prompt to be displayed to the user
o a minimum legal int value for input
o a maximum legal int value for input
- It returns a legal int value, based on the minimum and maximum values
- Includes a try catch statement to catch ValueErrors
The definition of the function is:
The pseudo-code for the function is below:
Loop forever
Try
Prompt user for input. Assign input to a variable
Check to see if the input is in the range of the min and max arguments
If legal, return the number
If not legal, print an error message
Except ValueError
Print error message to user
Example calls to the functions are:
In the above case, getNumber() would display “How many grades are you going to enter?” to
the user. It would only allow a number that is between 1 and 80 inclusive as legal input. If
other input is entered, the program should display a message and re-prompt the user for input.
Once a legal value is entered getNumber() will return the value and assign it to numGrades.
Another example call:
In the above case, getNumber() would display “Enter a grade between 0 and 100: ” to the user.
It would only allow a number that is between 0 and 100 inclusive as legal input. If other input is
entered, the program should display a message and re-prompt the user for input. Once a legal
value is entered getNumber() will return the value and assign it to grade.
NOTE: you can assume in this example that grades are integers (as opposed to floats).
Your final program should include:
- The function getNumber()
- Two sample calls to the function. Use the two examples above.
- A print statement displaying both inputted values within a semantic message.
Example output (with sample illegal values entered) is shown below.
Save your file as: ICE-3_cuid.py where cuid is your Carleton University Identification. Next save
your file as: ICE-3_cuid.txt . Upload the .txt version to Brightspace.
BONUS
Extend your program so that:
- It includes a main() function which includes the calls to getNumber()
- It includes a loop that will prompt the user for all the grade information. For
example, if I say I am going to enter four grades, I would get prompted to enter all
four wages
- Store the entered grade information in a list named GradeInfo.
- Change your output message per the sample output below.
Save your file as: ICE-3_Bonus_cuid.py where cuid is your Carleton University Identification.
Next save your file as: ICE-3_Bonus_cuid.txt . Upload the .txt version to Brightspace.
Download