0% found this document useful (0 votes)
3 views1 page

Python Assignment 2

The document outlines five programming exercises focused on error checking and input validation. It includes tasks for calculating averages, sums of integers, and factorials while ensuring correct data types and value constraints. Each exercise emphasizes user interaction and appropriate error messages for invalid inputs.

Uploaded by

ahruranj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Python Assignment 2

The document outlines five programming exercises focused on error checking and input validation. It includes tasks for calculating averages, sums of integers, and factorials while ensuring correct data types and value constraints. Each exercise emphasizes user interaction and appropriate error messages for invalid inputs.

Uploaded by

ahruranj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Error Checking Practice

1) Write a program using try/except to ensure that the datatype of the inputted data is correct

Create your own input prompts and error messages.


-Ask a user to enter four numbers
-Output the average of the numbers.

2) Write a program that calculates the sum of the first n integers as shown below.
 Make sure your program does two error checks:
1) checks datatype of input
2) checks that the integer entered is > 0
 Your program should loop until the user enters an appropriate number

Suggested Sample Program Interaction:


This program will add up all the numbers from 1 until the number you enter.

Enter number: mmm, Oreo cookies... my favourite!


Invalid input! Please enter a positive integer greater than 0!

Enter number: 10
The sum of the first 10 integers is 55.

3) Write a program that calculates the average for an unknown number of temperatures.
Repeatedly ask for input until the user enters “Done”.
Make sure your program checks for data type – are there any additional checks?

#Sample run:

Enter the temperature: -14.3


Enter the temperature: 34
Enter the temperature: -6
Enter the temperature: Done

The average temperature is 4.6 degrees Celsius.

For the last two questions, design your own sample interactions and error messages.

4) Ask the user for a positive integer and calculate the factorial of that integer.

5) Ask the user for a positive integer and calculate the factorial of that integer.
This time, make sure the integer is not less than 0 and no greater than 50.
(0 and 50 may be valid inputs)

You might also like