8.19 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
10.8 LAB: Exception handling to detect input string vs. integerThe 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
in python please 108 lab exception handling to detect input string vs integer the given program
How to use Try and Except to detect if user has entered stringb or integer?
8 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
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 …
Videos
Stuck on this part of the practice project in the book automate the boring the stuff. It asks you to add try and except statements to detect if the user has entered an integer or noninteger string but doesn't explain anywhere the actual syntax for how to do that.
Save the input in a variable and convert to an integer separately:
Copyimport sys
i = input("Please enter the exam mark out of 100 ")
try:
mark = int(i)
except ValueError:
print('\nYou did not enter a valid integer')
sys.exit(0)
if mark < 60:
print("\nFail")
elif mark < 101:
print("\nPass")
else:
print("\nThe mark is out of range")
If it fails (i.e., you get a ValueError) then print a message and exit. You can explain (to a 14-year old) that int() needs a valid integer as input and it will raise a ValueError otherwise. That makes sense because only strings that contain an integer can be converted by int().
Copytry:
mark = int(input("Please enter the exam mark out of 100 "))
except ValueError:
print("\nPlease only use integers")
I'm working on a Python lesson on exception handling. I need to edit a code to handle exceptions, but I can't get it to work. It needs to detect an invalid string and input "0" as instead of the invalid information. My current code is returning an error of Traceback (most recent call last): File "main.py", line 11, in <module> age = int(parts[1]) + 1 ValueError: invalid literal for int() with base 10: 'Beth'
Ex input:
Lee 18 Lua 21 Mary Beth 19 Stu 33 -1
Expected output:
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:
print('{} {}'.format(name, age))
except:
print('0')# FIXME: The following line will throw ValueError exception.
age = int(parts[1]) + 1
print('{} {}'.format(name, age))
parts = input().split()
name = parts[0]