🌐
Stack Overflow
stackoverflow.com › questions › 71190373 › python-detect-string-and-int-using-exception-handling
Python detect string and int using exception handling - Stack Overflow
parts = input().split() name = parts[0] while name != '-1': try: age = int(parts[1]) + 1 except ValueError: age = 0 print('{} {}'.format(name, age)) parts = input().split() name = parts[0] Basically instead of identifying the ValueError, we're just assigning age with 0 and continuing on. This is essentially working as an if/else. If age isn't an integer, then assign age with 0.
Discussions

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
I need this answer in Python please! ... There are 2 steps to solve this one. ... 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. More on chegg.com
🌐 chegg.com
1
June 25, 2022
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
Answer to 10.8 LAB: Exception handling to detect input string More on chegg.com
🌐 chegg.com
1
November 13, 2023
in python please 108 lab exception handling to detect input string vs integer the given program
Numerade offers video solutions for the most popular textbooks More on numerade.com
🌐 numerade.com
1
November 2, 2024
How to use Try and Except to detect if user has entered stringb or integer?
It's worth checking the official docs for a proper explanation, but for this particular case, your syntax would look something like this: try: # code that might cause a ValueError. except ValueError: # code that will happen only if a ValueError occurred above. # code that will happen regardless of whether a ValueError occurred (i.e., your program won't crash). You can leverage this concept to selectively run code if an attempt is made to convert a non-numeric string to an integer. More on reddit.com
🌐 r/learnpython
7
0
March 22, 2023
People also ask

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
The Answer is in the below Steps
🌐
bartleby.com
bartleby.com › bartleby textbook solutions › engineering q&a and textbook solutions › computer science q&a, textbooks, and solutions › computer science q&a library › 20.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
Answered: 20.8 LAB: Exception handling to detect input string vs. ...
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 …
Here I have taken input from the user and then stored it into a variable by splitting them on the…
🌐
bartleby.com
bartleby.com › bartleby textbook solutions › engineering q&a and textbook solutions › computer science q&a, textbooks, and solutions › computer science q&a library › 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 …
Answered: 4.10 LAB: Exception handling to detect input string vs. ...
🌐
Brainly
brainly.com › computers and technology › high school › python: 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 ``` **code given:** ```python # 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] ``` **my code:** ```python # split input into 2 parts: name and age parts = input().split() name = parts[0] while name != '-1': try: # the following line will throw valueerror exception. age = int(parts[1]) + 1 except valueerror: age = 0 print('{} {}'.format(name, age)) # get next line parts = input().split() name = parts[0] ``` **my error message:** ``` file "main.py", line 19 parts = input().split() ^ syntaxerror: invalid syntax ``` i don't understand what i'm doing wrong. can someone help me see what i'm doing wrong in my code? thank you.
[FREE] PYTHON: 4.10 LAB: Exception handling to detect input string vs. integer The given program reads a list of - brainly.com
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 18lua ...
🌐
SlideShare
slideshare.net › home › education › python exception handling detects string vs integer input
Python exception handling detects string vs integer input | PDF
February 23, 2023 - 1. In python 10.8 LAB: Exception ... 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....
🌐
Bartleby
bartleby.com › bartleby textbook solutions › engineering q&a and textbook solutions › computer science q&a, textbooks, and solutions › computer science q&a library › 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 …
Answered: 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… | bartleby
June 22, 2022 - Transcribed Image Text:### LAB: ... exceptions when converting input strings to integers. The goal is to split the input into parts, calculate the age, and handle any errors that occur during conversion....
Find elsewhere
🌐
AskPython
askpython.com › home › handling valueerror in python: detecting strings and integers
Handling ValueError in Python: Detecting Strings and Integers - AskPython
April 29, 2023 - ... Let’s look at how we can use the try and except clauses, to handle ValueErrors. Let’s implement the try and except clauses for ValueError handling. # importing required module import math var = input("Enter number= ") # exception handling ...
🌐
Assignment Access
assignmentaccess.com › ExpertAnswers › iin-python-please-10-8-lab-exception-handling-to-detect-input-string-vs-integer-the-given-program-re
Solved: iIN PYTHON PLEASE 10.8 LAB: Exception handling to | assignmentaccess.com
10.8 LAB: Exception handling to ... 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....
🌐
Reddit
reddit.com › r/codinghelp › python exception handling
r/CodingHelp on Reddit: Python Exception Handling
October 15, 2021 -

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]