Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program

The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a string rather than an integer. At FIXME in the code, add try and except blocks to catch the ValueError exception and output 0 for the age.

Ex: If the input is:

Lee 18
Lua 21
Mary Beth 19
Stu 33
-1
then the output is:

Lee 19
Lua 22
Mary 0
Stu 34
# Split input into 2 parts: name and age

parts = input().split()

name = parts[0]

while name != '-1':

# FIXME: The following line will throw ValueError exception.

# Insert try/except blocks to catch the exception.

age = int(parts[1]) + 1

print('{} {}'.format(name, age))

# Get next line

parts = input().split()

name = parts[0]

Step by Step Solution

3.53 Rating (146 Votes )

There are 3 Steps involved in it

Step: 1

The try and expect blocks in python is used to catch and handle exc... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Labor and Employment Law Text and Cases

Authors: David Twomey

15th edition

1133188281, 978-1133711841, 1133711847, 978-1285247632, 978-1133188285

More Books

Students also viewed these Programming questions

Question

Question

Answered: 1 week ago