Question

The program below successfully asks a user to enter 2 numbers and calculates a mean. BUT,...

The program below successfully asks a user to enter 2 numbers and calculates
a mean. BUT, if the user enters something that isn't a number, the program
crashes. Catch this error using a try/except block.

accum = 0

for i in range(2):
num = int(input("Enter your number:"))
accum = accum + num
mean = accum/2
print("The average is ",mean)

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

Code image:

Output image:

Code :

try:
x = int(input("Enter a number : "))
y = int(input("Enter a number : "))
mean = (x + y)/2
print("Mean : ", mean)
except ValueError:
print("Invalid input.")

Note :

Please follow indentation(spaces/tabs) according to the code image.

Add a comment
Know the answer?
Add Answer to:
The program below successfully asks a user to enter 2 numbers and calculates a mean. BUT,...
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
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