4.10 LAB: Exception handling to detect input string vs. integer 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': try: age = int(parts[1]) + 1 except: age - 1 print('{} {}'.format(name, age)) parts = input().split() name = parts[0] Traceback Total score: 0 / 10 Only show failing tests Download this submission 1: Compare outputkeyboard_arrow_up 0 / 1 Output differs. See highlights below. Input Lee 18 Lua 21 Mary Beth 19 Stu 33 -1 Your output Lee 19 Lua 22 Mary 22 Stu 34 Expected output Lee 19 Lua 22 Mary 0 Stu 34 2: Compare outputkeyboard_arrow_up 0 / 3 Output differs. See highlights below. Input Laura 63 Vaishnavi 24 Sarah Sims 33 -1 Your output Laura 64 Vaishnavi 25 Sarah 25 Expected output Laura 64 Vaishnavi 25 Sarah 0 3: Compare outputkeyboard_arrow_up 0 / 3 Output differs. See highlights below. Input Huw 29 Jaspar 49 Melina Lynn 32 Quinta 13 Mina Ny 38 Hanna 28 -1 Your output Huw 30 Jaspar 50 Melina 50 Quinta 14 Mina 14 Hanna 29 Expected output Huw 30 Jaspar 50 Melina 0 Quinta 14 Mina 0 Hanna 29 4: Compare outputkeyboard_arrow_up 0 / 3 Traceback (most recent call last): File "main.py", line 6, in age = int(parts[1]) + 1 ValueError: invalid literal for int() with base 10: 'Jean' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "main.py", line 8, in age - 1 NameError: name 'age' is not defined Input Laura Jean 17 Christine 55 Felicia 31 Kofi Drew 39 Margaux 98 -1 Your output Your program produced no output Expected output Laura 0 Christine 56 Felicia 32 Kofi 0 Margaux 99
4.10 LAB: Exception handling to detect input string vs. integer 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': try: age = int(parts[1]) + 1 except: age - 1 print('{} {}'.format(name, age)) parts = input().split() name = parts[0] Traceback Total score: 0 / 10 Only show failing tests Download this submission 1: Compare outputkeyboard_arrow_up 0 / 1 Output differs. See highlights below. Input Lee 18 Lua 21 Mary Beth 19 Stu 33 -1 Your output Lee 19 Lua 22 Mary 22 Stu 34 Expected output Lee 19 Lua 22 Mary 0 Stu 34 2: Compare outputkeyboard_arrow_up 0 / 3 Output differs. See highlights below. Input Laura 63 Vaishnavi 24 Sarah Sims 33 -1 Your output Laura 64 Vaishnavi 25 Sarah 25 Expected output Laura 64 Vaishnavi 25 Sarah 0 3: Compare outputkeyboard_arrow_up 0 / 3 Output differs. See highlights below. Input Huw 29 Jaspar 49 Melina Lynn 32 Quinta 13 Mina Ny 38 Hanna 28 -1 Your output Huw 30 Jaspar 50 Melina 50 Quinta 14 Mina 14 Hanna 29 Expected output Huw 30 Jaspar 50 Melina 0 Quinta 14 Mina 0 Hanna 29 4: Compare outputkeyboard_arrow_up 0 / 3 Traceback (most recent call last): File "main.py", line 6, in age = int(parts[1]) + 1 ValueError: invalid literal for int() with base 10: 'Jean' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "main.py", line 8, in age - 1 NameError: name 'age' is not defined Input Laura Jean 17 Christine 55 Felicia 31 Kofi Drew 39 Margaux 98 -1 Your output Your program produced no output Expected output Laura 0 Christine 56 Felicia 32 Kofi 0 Margaux 99
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter14: Exception Handling
Section: Chapter Questions
Problem 22SA
Related questions
Question
100%
4.10 LAB: Exception handling to detect input string vs. integer
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 -1then 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':
try:
age = int(parts[1]) + 1
except:
age - 1
print('{} {}'.format(name, age))
parts = input().split()
name = parts[0]
parts = input().split()
name = parts[0]
while name != '-1':
try:
age = int(parts[1]) + 1
except:
age - 1
print('{} {}'.format(name, age))
parts = input().split()
name = parts[0]
Traceback
Total score: 0 / 10
Only show failing tests
1: Compare outputkeyboard_arrow_up
0 / 1Output differs. See highlights below.
Input
Lee 18 Lua 21 Mary Beth 19 Stu 33 -1
Your output
Lee 19 Lua 22 Mary 22 Stu 34
Expected output
Lee 19 Lua 22 Mary 0 Stu 34
2: Compare outputkeyboard_arrow_up
0 / 3Output differs. See highlights below.
Input
Laura 63 Vaishnavi 24 Sarah Sims 33 -1
Your output
Laura 64 Vaishnavi 25 Sarah 25
Expected output
Laura 64 Vaishnavi 25 Sarah 0
3: Compare outputkeyboard_arrow_up
0 / 3Output differs. See highlights below.
Input
Huw 29 Jaspar 49 Melina Lynn 32 Quinta 13 Mina Ny 38 Hanna 28 -1
Your output
Huw 30 Jaspar 50 Melina 50 Quinta 14 Mina 14 Hanna 29
Expected output
Huw 30 Jaspar 50 Melina 0 Quinta 14 Mina 0 Hanna 29
4: Compare outputkeyboard_arrow_up
0 / 3Traceback (most recent call last): File "main.py", line 6, in <module> age = int(parts[1]) + 1 ValueError: invalid literal for int() with base 10: 'Jean' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "main.py", line 8, in <module> age - 1 NameError: name 'age' is not defined
Input
Laura Jean 17 Christine 55 Felicia 31 Kofi Drew 39 Margaux 98 -1
Your output
Your program produced no output
Expected output
Laura 0 Christine 56 Felicia 32 Kofi 0 Margaux 99
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning