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
icon
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 -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 <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
 
CTU Class Homepage.
zy Section 4.10 - CS 219T: Python Pr X
CTU Messenger
Section 3.6 CS 219T: Python Pro X
AST
✰ learn.zybooks.com/zybook/CS219-2203A-03-2203A/chapter/4/section/10
=zyBooks My library > CS 219T: Python Programming home > 4.10: LAB: Exception handling to detect input string vs. integer
zyBooks catalog
LAB
4.10.1: LAB: Exception handling to detect input string vs. integer
0/10
ACTIVITY
main.py
Load default template...
1 # Split input into 2 parts: name and age
2 parts
input().split()
3 name parts[0]
4 while name != '-1':
5
try:
6
age int (parts[1]) + 1
7
except:
8
age 1
9
print('{} {}'. format (name, age))
parts = input().split()
name parts[0]
Submit mode
Run your program as often as you'd like, before submitting for grading. Below, type any needed
input values in the first box, then click Run program and observe the program's output in the
second box.
Input (from above)
main.py
(Your program)
Output (shown below)
10
11
12
13
14
Develop mode
Enter program input (optional)
Lee 18
Lua 21
Run program
b Success Confirmation of Questic X
"!
? Help/FAQ
Stacey Queen
X
:
9:36 PM
6/21/2022
Transcribed Image Text:CTU Class Homepage. zy Section 4.10 - CS 219T: Python Pr X CTU Messenger Section 3.6 CS 219T: Python Pro X AST ✰ learn.zybooks.com/zybook/CS219-2203A-03-2203A/chapter/4/section/10 =zyBooks My library > CS 219T: Python Programming home > 4.10: LAB: Exception handling to detect input string vs. integer zyBooks catalog LAB 4.10.1: LAB: Exception handling to detect input string vs. integer 0/10 ACTIVITY main.py Load default template... 1 # Split input into 2 parts: name and age 2 parts input().split() 3 name parts[0] 4 while name != '-1': 5 try: 6 age int (parts[1]) + 1 7 except: 8 age 1 9 print('{} {}'. format (name, age)) parts = input().split() name parts[0] Submit mode Run your program as often as you'd like, before submitting for grading. Below, type any needed input values in the first box, then click Run program and observe the program's output in the second box. Input (from above) main.py (Your program) Output (shown below) 10 11 12 13 14 Develop mode Enter program input (optional) Lee 18 Lua 21 Run program b Success Confirmation of Questic X "! ? Help/FAQ Stacey Queen X : 9:36 PM 6/21/2022
CTU Class Homepage.
zy Section 4.10 - CS 219T: Python Pr X
CTU Messenger
Section 3.6 CS 219T: Python Pro X
AST
✰ learn.zybooks.com/zybook/CS219-2203A-03-2203A/chapter/4/section/10
=zyBooks My library > CS 219T: Python Programming home > 4.10: LAB: Exception handling to detect input string vs. integer
Mina 0
Hanna 29
4: Compare output
Traceback (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
Laura Jean 17
Christine 55
Felicia 31
Input
Kofi Drew 39
Margaux 98
-1
Your output Your program produced no output
Laura 0
Christine 56
Expected output
Felicia 32
Kofi 0
b Success Confirmation of Questic X
zyBooks catalog
0/3
? Help/FAQ
Stacey Queen
X
:
9:37 PM
6/21/2022
Transcribed Image Text:CTU Class Homepage. zy Section 4.10 - CS 219T: Python Pr X CTU Messenger Section 3.6 CS 219T: Python Pro X AST ✰ learn.zybooks.com/zybook/CS219-2203A-03-2203A/chapter/4/section/10 =zyBooks My library > CS 219T: Python Programming home > 4.10: LAB: Exception handling to detect input string vs. integer Mina 0 Hanna 29 4: Compare output Traceback (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 Laura Jean 17 Christine 55 Felicia 31 Input Kofi Drew 39 Margaux 98 -1 Your output Your program produced no output Laura 0 Christine 56 Expected output Felicia 32 Kofi 0 b Success Confirmation of Questic X zyBooks catalog 0/3 ? Help/FAQ Stacey Queen X : 9:37 PM 6/21/2022
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Exception Handling Keywords
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.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning