369,706 views
13 votes
13 votes
You need to write a menu driven program. The program allows a user to enter five numbers and then asks the user to select a choice from a menu. The menu should offer the following four options – 1. Display the smallest number entered 2. Display the largest number entered 3. Display the sum of the five numbers entered 4. Display the average of the five numbers entered

User David Duponchel
by
3.0k points

1 Answer

16 votes
16 votes

Answer:

In Python:

nums = []

for i in range(5):

num = int(input("Num: "))

nums.append(num)

print("1 - Smallest")

print("2 - Largest")

print("3 - Sum")

print("4 - Average")

menu = int(input("Select menu: "))

if menu == 1:

print("Smallest: ",min(nums))

elif menu == 2:

print("Largest: ",max(nums))

elif menu == 3:

isum = 0

for i in range(5):

isum+=nums[i]

print("Sum: ",isum)

elif menu == 4:

isum = 0

for i in range(5):

isum+=nums[i]

print("Average: ",isum/5)

else:

print("Invalid Menu Selected")

Step-by-step explanation:

This program uses a list to get inputs for the 5 numbers

Here, the list is initialized

nums = []

This iterates from 1 to 5

for i in range(5):

This gets input for the 5 numbers

num = int(input("Num: "))

This appends each number to the list

nums.append(num)

The next 4 lines represents the menu

print("1 - Smallest")

print("2 - Largest")

print("3 - Sum")

print("4 - Average")

This prompts the user for menu

menu = int(input("Select menu: "))

If menu is 1, print the smallest

if menu == 1:

print("Smallest: ",min(nums))

If menu is 2, print the largest

elif menu == 2:

print("Largest: ",max(nums))

If menu is 3, calculate and print the sum of all inputs

elif menu == 3:

isum = 0

for i in range(5):

isum+=nums[i]

print("Sum: ",isum)

If menu is 4, calculate and print the average of all inputs

elif menu == 4:

isum = 0

for i in range(5):

isum+=nums[i]

print("Average: ",isum/5)

If menu is not 1 to 4, then print invalid menu

else:

print("Invalid Menu Selected")

User Gerardo Tarragona
by
2.5k points