Knowee
Questions
Features
Study Tools

WAP to create an array of 6 integer elements use try and catch block to show exceptionoccured when(i)User enter other than integer throw exception and assign rest value to 0.(ii)user enter other than integer throw exception and assign only that value as 0.

Question

WAP to create an array of 6 integer elements use try and catch block to show exceptionoccured when(i)User enter other than integer throw exception and assign rest value to 0.(ii)user enter other than integer throw exception and assign only that value as 0.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python program that fulfills your requirements:

def input_integer():
    try:
        return int(input("Enter an integer: "))
    except ValueError:
        print("You entered a non-integer value. It will be replaced with 0.")
        return 0

array = [0]*6

for i in range(6):
    array[i] = input_integer()

print("The final array is:", array)

This program creates an array of 6 elements. It uses a function input_integer to get user input. This function tries to convert the user input to an integer. If the user enters a non-integer value, a ValueError exception is thrown. The exception is caught and a message is printed to the user. The function then returns 0. This process is repeated 6 times to fill the array. The final array is then printed.

This problem has been solved

Similar Questions

WAP that asks the user to enter a number one at a time until the user hits exactly zero (0).Display the sum of all the numbers that were entered by the user before hitting zero.

WAP in C++ to implement array of objects. creating a class employee and accepting and displaying multiple datasets accepted by the user using array of objects.

You have 3 numbers as user input. WAP in Java to output their values in descending order.

Write a program to accept marks from 1-100as input from user. If the user enters <40as an input or if user enters any negative number, or >100  user defined exception should begenerated.

Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.