Write a Python program to perform the following tasks according to the user's choice using a menu.

a) Display the menu with four options: 1. Area of Triangle, 2. Area of Circle, 3. Perimeter of Rectangle, 4. Exit. (0.5 mark)

b) Ask the user to enter their choice and validate it. If the choice is invalid, display an error message and ask again. (0.5 mark)

c) If the choice is 1, ask the user to enter the base and height of a triangle and then calculate and print the area of the triangle using the formula: area = 0.5 * base * height. (1 mark)

d) If the choice is 2, ask the user to enter the radius of a circle and then calculate and print the area of the circle using the formula: area = 3.14 * radius * radius. (1 mark)

e) If the choice is 3, ask the user to enter the length and width of a rectangle and then calculate and print the perimeter of the rectangle using the formula: perimeter = 2 * (length + width). (1 mark)

f) If the choice is 4, exit the program with a goodbye message. (0.5 mark)

g) Use a loop to repeat the menu until the user chooses to exit. (0.5 mark)

Answer:

Answer by student

print( "Please choose one of the following options:" )

print( "1. Area of Triangle" )

print( "2. Area of Circle" )

print( "3. Perimeter of Rectangle" )

print( "4. Exit" )

 

while True :

  choice = input( "Enter your choice (1/2/3/4): " )

  if choice not in [ "1" , "2" , "3" , "4" ]:

    print( "Invalid choice. Please enter a valid choice." )

  else :

    if choice == "1" :

      base = float(input( "Enter the base of the triangle: " ))

      height = float(input( "Enter the height of the triangle: " ))

      area = 0.5 * base * height

      print( "The area of the triangle is" , area)

    elif choice == "2" :

      radius = float(input( "Enter the radius of the circle: " ))

      area = 3.14 * radius * radius

      print( "The area of the circle is" , area)

    elif choice == "3" :

      length = float(input( "Enter the length of the rectangle: " ))

      width = float(input( "Enter the width of the rectangle: " ))

      perimeter = 2 * (length + width)

      print( "The perimeter of the rectangle is" , perimeter)

    elif choice == "4" :

      print( "Thank you for using this program. Goodbye!" )

      break

Detailed answer by teachoo

  • We first display the menu with four options using four print statements. Each print statement takes a string as an argument and displays it on the screen.
  • We then use a while loop to repeat the menu until the user chooses to exit. The while loop has a condition that is always True , so it will run indefinitely unless we break out of it.
  • Inside the while loop, we ask the user to enter their choice using an input statement and store it in a variable called choice. The input statement takes a string as an argument and displays it as a prompt for the user. The input statement returns whatever the user types as a string.
  • We then validate the user input using an if statement. The if statement has a condition that checks if the choice is not in [“1”, “2”, “3”, “4”] using the not and in operators . If yes, it executes a print statement that displays an error message and asks again. If no, it executes an else clause that contains another if-elif-else statement.
  • The nested if-elif-else statement checks which option is chosen by the user using four conditions that compare the choice with “1”, “2”, “3” and “4” using the == operator. If any condition is True, it executes a corresponding block of code that performs a specific task. If none of them is True, it does nothing.
  • If the choice is “1”, we ask the user to enter the base and height of a triangle using two input statements and store them in two variables: base and height. The input statements take strings as arguments and display them as prompts for the user. The input statements return whatever the user types as strings. We convert them to floating-point numbers using the float() function and assign them to base and height variables using assignment statements.
  • We then calculate and print the area of the triangle using two statements. We use an arithmetic expression with multiplication and division operators to compute 0.5 * base * height and assign it to a variable called area using an assignment statement. We use a print statement with comma-separated arguments to display “The area of the triangle is” followed by  the value of area.
  • If the choice is “2” , we ask the user to enter the radius of a circle using an input statement and store it in a variable called radius. The input statement takes a string as an argument and displays it as a prompt for the user. The input statement returns whatever the user types as a string. We convert it to a floating-point number using the float() function and assign it to radius variable using an assignment statement.
  • We then calculate and print the area of the circle using two statements. We use an arithmetic expression with multiplication operator to compute 3.14 * radius * radius and assign it to a variable called area using an assignment statement. We use a print statement with comma-separated arguments to display “The area of the circle is” followed by the value of area.
  • If the choice is “3” , we ask the user to enter the length and width of a rectangle using two input statements and store them in two variables: length and width. The input statements take strings as arguments and display them as prompts for the user. The input statements return whatever the user types as strings. We convert them to floating-point numbers using the float () function and assign them to length and width variables using assignment statements.
  • We then calculate and print the perimeter of the rectangle using two statements. We use an arithmetic expression with addition and multiplication operators to compute 2 * (length + width) and assign it to a variable called perimeter using an assignment statement. We use a print statement with comma-separated arguments to display “The perimeter of the rectangle is” followed by the value of perimeter.
  • If the choice is “4” , we exit the program with a goodbye message using a print statement and a break statement. The print statement takes a string as an argument and displays it on the screen. The break statement terminates the while loop and exits the program.
  • This way, we perform the tasks according to the user’s choice using menu.

So, the final code is:

# display the menu with four options

print( "Please choose one of the following options:" )

print( "1. Area of Triangle" )

print( "2. Area of Circle" )

print( "3. Perimeter of Rectangle" )

print( "4. Exit" )

 

# use a loop to repeat the menu until the user chooses to exit

while True :

  # ask the user to enter their choice and validate it

  choice = input( "Enter your choice (1/2/3/4): " )

  if choice not in [ "1" , "2" , "3" , "4" ]:

    # display an error message and ask again

    print( "Invalid choice. Please enter a valid choice." )

  else :

    # check which option is chosen by the user

    if choice == "1" :

      # ask the user to enter the base and height of a triangle

      base = float(input( "Enter the base of the triangle: " ))

      height = float(input( "Enter the height of the triangle: " ))

      # calculate and print the area of the triangle

      area = 0.5 * base * height

      print( "The area of the triangle is" , area)

    elif choice == "2" :

      # ask the user to enter the radius of a circle

      radius = float(input( "Enter the radius of the circle: " ))

      # calculate and print the area of the circle

      area = 3.14 * radius * radius

      print( "The area of the circle is" , area)

    elif choice == "3" :

      # ask the user to enter the length and width of a rectangle

      length = float(input( "Enter the length of the rectangle: " ))

      width = float(input( "Enter the width of the rectangle: " ))

      # calculate and print the perimeter of the rectangle

      perimeter = 2 * (length + width)

      print( "The perimeter of the rectangle is" , perimeter)

    elif choice == "4" :

      # exit the program with a goodbye message

      print( "Thank you for using this program. Goodbye!" )

      break
Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo