Question

Implement a program (in both C# and Python) which includes a user-defined exception giving a message...

Implement a program (in both C# and Python) which includes a user-defined exception giving a message “This score is too low. You failed the exam.” if a score is less than 50. Otherwise, it should give a message “You passed the exam. Congratulations.”.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C# CODE

Code to copy:

// C# CODE:

using System;

// user-defined exception class

class LowResult : Exception

{

// base is used to access members of the base class

// All exceptions inherit from System.Exception class

public LowResult() : base("This score is too low. You failed the exam.")

{}

}

// main class

class MainClass

{

public static void Main (string[] args)

{

try

{

// prompt user to enter the score

Console.WriteLine("Enter the Score: ");

// read the user input as int

// Convert.ToInt32() converts string to int

int score = Convert.ToInt32(Console.ReadLine());

// Check if score is less than 50

if(score < 50)

{

// throw the exception that arises

throw new LowResult();

}

else

{

// display the student passed the exam

Console.WriteLine("You passed the exam. Congratulations.");

}

}

// catch the error thrown within the try-block

catch(LowResult e)

{

// display the error message

Console.WriteLine(e.Message);

}

}

}

Screenshot of the code:

Sample Output:

Test case 1

Test case 2

PYTHON CODE

Code to copy:

# PYTHON CODE:

# User-defined class for low score

class LowResult(Exception):

pass

# main program

try:

# prompt to enter score

score = int(input("Enter the Score: "))

# check if the score is less than 50

if score < 50:

# raise the defined error

raise LowResult()

else:

# display the student passed

print("You passed the exam. Congratulations.")

except:

# display the error message

print("This score is too low. You failed the exam.")

Screenshot of the code:

Sample Output:

Test case 1

Test case 2

Add a comment
Know the answer?
Add Answer to:
Implement a program (in both C# and Python) which includes a user-defined exception giving a message...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • In Python. Complete the following programs. Begin each program with a comment that includes: • Your...

    In Python. Complete the following programs. Begin each program with a comment that includes: • Your name • The project/program • Brief description of the problem Lab3Pro1 A supervisor in a manufacturing company wishes to display the bonus amount for an employee based on this year's production. Input Output Processing Distribute bonuses as follows: Display the results as follows: Prompt for user input "Enter number of Units produced" Store the amount entered Bonus This year's production bonus will be $nnnnn...

  • In c# create a program that generates a random number from 1 to 1000. Then, ask...

    In c# create a program that generates a random number from 1 to 1000. Then, ask the user to guess the random number. If the user's guess is too high, then the program should print "Too High, Try again". If the user's guess is too low, then the program should print "Too low, Try again". Use a loop to allow the user to keep entering guesses until they guess the random number correctly. Once they figure it, congratulate the user....

  • Matlab

    Part A: Quiz ApplicationIn this part, you are required to implement Quiz Application. Add 10 questions in your program of any domain with their answers. You many use strcmpi function to compare user answer with the saved answer. If the answer is correct/matched with the saved answer user score 1 point.  Functionalities that need to be implemented in Quiz application are: -1.     The user will be asked to enter the name and then asked questions one by one. 2.     The...

  • Write python code on computer, No handwritten code You will implement a program which asks the...

    Write python code on computer, No handwritten code You will implement a program which asks the user to enter scores of different courses in different semesters. The program should read the input and output some simple statistics 1. An example input string by the user is given below. Fal12016-coursel:82, course2:80,course3:85;Spring2018- course4:82;Fall2018-course5:85, course6:50, course7:78 Info from different semesters are separated by a ";", courses in each semester are separated by a,and score and corresponding course is separated by a, information of...

  • Write a Python program that asks the user to enter the number of calories and fat...

    Write a Python program that asks the user to enter the number of calories and fat grams in a food item. The program should display the percentage of the calories that come from fat. One gram of fat has 9 calories, therefore: Calories from fat = fat grams * 9 The percentage of calories from fat can be calculated as follows: Calories from fat / total calories If the calories from fat are less than 30 percent of the total...

  • C++ Programming Question

    The Maybe High School offers a course that prepares students for tertiary-level education. Last year, several students who completed the course matriculated to a tertiary level institution of their choice. Naturally, the college wants to know how well its students did on the exam. You have been asked to write a program to summarize the results. Your program should: Prompt the user to enter the final grades of an indefinite number of students. Note: A final grade greater than or equal to 75 is considered a...

  • python question Question 1 Write a Python program to play two games. The program should be...

    python question Question 1 Write a Python program to play two games. The program should be menu driven and should use different functions to play each game. Call the file containing your program fun games.py. Your program should include the following functions: • function guess The Number which implements the number guessing game. This game is played against the computer and outputs the result of the game (win/lose) as well as number of guesses the user made to during the...

  • python question 2. In a file called passfail.py, write a Python program to solve the following...

    python question 2. In a file called passfail.py, write a Python program to solve the following problem: Given a file students.txt with the following information for a stu dent: student number, average quiz mark, average assignment mark, midterm exam mark, and final exam mark (where each mark is recorded out of 100), de termine if the student has passed or failed based on the following information: • A student will pass the course if they have a passing mark (50%...

  • PYTHON CODE PLEASEEEEE. Driver’s License Exam 20PTS PYTHON AND FLOWCHART The local driver’s license office has...

    PYTHON CODE PLEASEEEEE. Driver’s License Exam 20PTS PYTHON AND FLOWCHART The local driver’s license office has asked you to design a program that grades the written portion of the driver’s license exam. The exam has 20 multiple choice questions. Here are the correct answers: 1.B 6.A 11.B 16.C 2.D 7.B 12.C 17.C 3.A 8.A 13.D 18.B 4.A 9.C 14.A 19.D 5.C 10.D 15.D 20.A Your program should store these correct answers in an array. (Store each question’s correct answer in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT