0% found this document useful (0 votes)
85 views

Homework Three

The document provides guidelines for a homework assignment involving Python programming questions. It specifies that code should be in Jupyter notebooks, name conventions, handling exceptions gracefully, and avoiding plagiarism. It then describes 4 programming questions - reading and applying math operators to numbers from files, computing averages and joining sentences from a file, taking reciprocals of user-entered integers, and defining an absolute value function for user-entered numbers while handling exceptions. Sample interactions are provided for each question.

Uploaded by

Pratik Bhavsar
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)
85 views

Homework Three

The document provides guidelines for a homework assignment involving Python programming questions. It specifies that code should be in Jupyter notebooks, name conventions, handling exceptions gracefully, and avoiding plagiarism. It then describes 4 programming questions - reading and applying math operators to numbers from files, computing averages and joining sentences from a file, taking reciprocals of user-entered integers, and defining an absolute value function for user-entered numbers while handling exceptions. Sample interactions are provided for each question.

Uploaded by

Pratik Bhavsar
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
You are on page 1/ 6

Rabih Neouchi Fall 2021 MIS 6382

JSOM, UTD Due by: 11/2/2021

Homework Three
The following guidelines must be followed and will be used to grade your homework:

• The code for each question should be implemented using Jupiter notebooks.
• All the code should be included in one single notebook (.ipynb file).
• Sample runs shown in the question should be used as a guide for implementation.
However extensive testing needs to be done on your code to deal with all test cases that
might possibly be executed.
• No credit will be given to hardcoded test cases within the code.
• Please abide by your academic integrity. This is an individual homework assignment.
• No group submissions will be accepted.
• You should name your “.ipynb” file using “FirstnameLastnameHW3”.
• You will get zero points if your program has syntax errors.

Page 1 | 6
Rabih Neouchi Fall 2021 MIS 6382
JSOM, UTD Due by: 11/2/2021

Question 1
(3.75 pts) Write a python program that does the following:
1. Reads a ‘numbers.txt’ file (2 lines at a time) that contains numbers (integers)
2. Reads a ‘operators.txt’ file (1 line at a time) that contains operators
3. Applies the operator to the two numbers just read
4. Displays the result to a file ‘output.txt’

Notes:
a. Your program should handle exceptions gracefully
b. If an operator is illegal to perform on a set of numbers, the program should skip the
numbers and the operators and move to the following set of numbers and operator
c. You can resume execution of the code once an exception is raised: just use
the pass or continue keywords inside the except block.

Sample Interaction:

File: numbers.txt File: operators.txt File: output.txt


(Read 2 lines at a (Read 1 line at a (Applies operator to two numbers read)
time) time)
30 / 30 / 6 = 5
6 + 15 + 16 = 31
15 * 12 * 10 = 1200
16 - 8-0=8
12 / 11 / 0 - Division by zero not allowed (ZeroDivisionError)
10 + b + 5 - Can not add characters (ValueError)
8 * 10 * 10 = 100
0 - 0 - 4 = -4
11
0
b
5
10
10
0
4

Page 2 | 6
Rabih Neouchi Fall 2021 MIS 6382
JSOM, UTD Due by: 11/2/2021

Question 2
(3.75 pts) Write a python program that does the following:
1. Reads a ‘input.txt’ file that contains numbers and sentences
2. Computes and displays the average of numerical values to a file ‘output.txt’.
3. Joins sentences using a ‘ .’ (space + dot) separator, and displays joined text to a file
‘output.txt’.

Notes:
a. Your program should handle empty lines as an empty string.
b. Hint: You can use the .join() function to concatenate or join sentences:
https://www.programiz.com/python-programming/methods/string/join
c. Assume only positive numerical values in the input file
d. If you have a blank line, you should just append ‘ .’ (space + dot) to the concatenated text

Sample Interaction:

File: input.txt File: output.txt


hi how are you The average of numerical values in the file is:
14.0
Hey
25 The concatenated text in the file is:
be right back hi how are you. . Hey. be right back. I like python programming!.
19 !/;@. . . --##--. See you soon!.
16
I like python programming!
!/;@
20
0
10

8
--##--
See you soon!

Page 3 | 6
Rabih Neouchi Fall 2021 MIS 6382
JSOM, UTD Due by: 11/2/2021

Question 3
(3.75 pts) We are going to implement the function inverse() that converts list of given integers to
1
its inverse. The inverse or reciprocal of a number 𝑥 is denoted by 𝑓(𝑥) = x.

Write a program that asks the user to enter a list of integers. The program should:
1. Raise and catch an exception if any value entered is non-integer. (the program should not
exit).
2. Define and implement a function that accepts an integer string as input, then returns the
reciprocal values of that list of integers. Make exception handling at the level of the
function.
3. Call your function by passing on the value the user entered
4. Loop continuously asking the user if they wish to continue (yes/no). If no is entered, the
program exits.

Sample Interaction:

Enter a list of integers separated by commas: a,4,-1,0


The entry is: a
Oops! Exception <class 'ValueError'> occurred.

The entry is: 4


The inverse of 4 is 0.25

The entry is: -1


The inverse of -1 is -1.0

The entry is: 0


Oops! Exception <class 'ZeroDivisionError'> occurred.

Do you want to continue? (yes/no): yes

Enter a list of integers separated by commas: 2,5


The entry is: 2
The inverse of 2 is 0.5

The entry is: 5


The inverse of 5 is 0.2

Do you want to continue? (yes/no): no

Page 4 | 6
Rabih Neouchi Fall 2021 MIS 6382
JSOM, UTD Due by: 11/2/2021

Question 4
(3.75 pts) We are going to implement a re-defined version of the absolute function abs() that
converts a number to its absolute value.

Write a program that asks the user to enter a number. The program should:

1- Catch a python exception if the value entered is not numerical. In this case, the user is
prompted to re-enter a valid number (the program should not exit).
2- Raise and catch a user-defined exception if the value entered contains special characters.
In this case, the user is prompted to re-enter a valid number (the program should not exit).
3- Define and implement a function that accepts a numerical string as input, then returns the
float value of that string. Make exception handling at the level of the function.
4- Call your function by passing on the value the user entered
5- Loop continuously asking the user if they wish to continue (yes/no). If no is entered, the
program exits.
6- Note: Your code can call the built-in abs() function.

Notes:
a. If a number contains multiple negative signs, your code should raise a user-defined
exception (contrary to the way the abs() function works in python).
b. Assume that the ‘.’ (dot) is not a special symbol
c. Assume that the ‘.’ (dot) alone is not going to be a test case.

Sample Interaction:

Please enter a number: hello


Caught a python exception. Please re-enter a valid number: /!hi
Caught a user-defined exception. Please re-enter a valid number: -20
The absolute value of your number is: 20
Would you like to continue? (yes/no): yes

Please enter a number: --10


Caught a user-defined exception. Please re-enter a valid number: ---15
Caught a user-defined exception. Please re-enter a valid number: 12
The absolute value of your number is: 12
Would you like to continue? (yes/no): yes

Please enter a number: -5


The absolute value of your number is: 5
Would you like to continue? (yes/no): yes

Please enter a number: 5@5


Caught a user-defined exception. Please re-enter a valid number: 12T

Page 5 | 6
Rabih Neouchi Fall 2021 MIS 6382
JSOM, UTD Due by: 11/2/2021

Caught a python exception. Please re-enter a valid number: -5b6


Caught a python exception. Please re-enter a valid number: bye
Caught a python exception. Please re-enter a valid number: 123.6
The absolute value of your number is: 123.6
Would you like to continue? (yes/no): yes

Please enter a number: 5.a


Caught a python exception. Please re-enter a valid number: &
Caught a user-defined exception. Please re-enter a valid number: !
Caught a user-defined exception. Please re-enter a valid number: -2.0
The absolute value of your number is: 2.0
Would you like to continue? (yes/no): no

Page 6 | 6

You might also like