Question: Q1) Write a python function that will remove a given character from a sentence based on the starting and ending index of the sentence. In

Q1) Write a python function that will remove a given character from a sentence based on the starting and ending index of the sentence. In the place of the removed characters, add the count of the number of characters removed so far. For this program, you need to take 4 user inputs.

  • Sentence
  • Character to be removed
  • Starting index (inclusive)
  • Ending index (inclusive)

Few exceptions to catch:

  • Name Error if a variable is not found. Print Name Error: Variable not defined
  • Value Error if the starting index and ending index is not an integer. Print Value Error: Wrong input, please enter an integer value.
  • Type Error when adding count in the new sentence. Print Type Error: Cannot add integer value with string.
  • Index Error if the starting and ending index is out of range. Print Index Error: Index is outside the range of sentence length.
  • For other exceptions, catch Exception. Print Some other error occurred.

If no exception occurred, print the new string using else block. At the end using finally, print The program execution is complete.

=============================

Example 1: Input: 'my python programming book' 'm' 2 20 Output: my python progra12ing book The program execution is complete

=================

Example 2: Input: 'my python programming book' 'm' 2.5 20 Output: Value Error: Wrong input, please enter an integer value. The program execution is complete

=================

Example 3: Input: 'my python programming book' 'm' 2 100 Output: Index Error: Index is outside the range of sentence length. The program execution is complete

=================

Hint(1): For ensuring starting index and ending index value to be integer use the builtin function isinstance(value, data_type)

Q2)

Write a python program to make a calculator. The user must input a string consisting of a number, operator and another number separated by white spaces.

  • If the users input does not contain 3 elements, raise an exception error stating "Input does not contain 3 elements/Wrong operator".

  • If the user inputs any other values except numbers in first and third position of the input, raise value error exception stating "Input does not contain numbers."

  • If the user does not input "+" or "-" or "/" or "*" or "%" in second position of the input, again raise an exception error stating "Input does not contain 3 elements/Wrong operator".

  • In case of division, also handle divide by zero exception.

If all the inputs are correct, calculate the result and print the value.

=============================

Example 1: Input: 1 + 1 + 1 Output: Input does not contain 3 elements/Wrong operator

=================

Example 2: Input: A * B Output: Input does not contain numbers.

=================

Example 3: Input: 1 $ 3 Output: Input does not contain 3 elements/Wrong operator

=================

Example 4: Input: 1 + 1 Output: The answer is : 2

Q3)

Read a file from the user which contains the name and salary of employees. Convert that to a dictionary where the key is the name of each employee and value is the salary of that employee. Then find the total cost for the company that it needs to pay through employees salary. Print the dictionary and sum if no exception occurs.

Exception to handle:

  • check whether file is actually there or not.
  • check whether the salary in each line is a number or not.
  • raise error if there are extra numbers or names

=============================

Example 1: Input: Saadat 100 Farzad 200 Atef 300 Output: dict={Saadat : 100, Farzad : 200, Atef : 300} Sum = 600

=================

Example 2: Input: Saadat 100 Farzad 200 Atef 300 John Output: Extra Statement

Example 3: Input: Saadat 100 Farzad 200 Atef john Output: Salary cannot be string

Q4)

Suppose you made a game and you stored information about the game play in a file. You stored Name, Score, Lives Used and Characters Killed of each players.

Existing file: Mary,2000,5,25 John,123455,12,50 Peter,1500,5,10 Harry,10000,10,30 John,122,5,2 Mary,5,1,1 Peter,2,1,1 John,123,5,10 John,5,6,7

Now you have to make a new file where you have to store the following information: 1.In the first line you have to show the name and score of the player who scored the highest score.

2.In the second line you have to store the name and lives used of players who used the least lives.

3.In the third line you have to show the name and number of games played of the player who played the most games.

Output: Name:John, Score:123455 Name:Mary, Lives used:1 Name:John, Games Played:4

=========================================================

Hint(1): Use dictionary where necessary. You are allowed to use max() min(), list.index(value) and str.split(separator) function.

Hint(2): You need to use split function to get the data. The split function takes the separator as an argument.

Structure: str.split(separator)

Example: line = "John,5,6,7" line.split(',')

Hint(3): You can use both write() and writelines() for writing the answers

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!