Since your were asking for some corrections or interpretations.

From your code

try:
    print("Abbreviation is ", bearNames[userBear])
    print("Attributes are ", bearAttributes[bearNames[userBear]])
    print("beartruth: ", beartruth)
    beartruth = False
    print("beartruth: ", beartruth)

except:
    print("Something went wrong - did you not type a bear name?")
    print("beartruth: ", beartruth)

You can be specific (and I would recommend it) with Exceptions to make sure you isolate the error that you may be expecting.

try:
    print("Abbreviation is ", bearNames[userBear])
except KeyError:
    print("Something went wrong - did you not type a bear name?")
    print("beartruth: ", beartruth)
else:
    print("Attributes are ", bearAttributes[bearNames[userBear]])
    print("beartruth: ", beartruth)
    beartruth = False
    print("beartruth: ", beartruth)

Doing it that way, you know that the Bear wasn't actually one. And only if the Bear is a real one you go into the else block to do something else.

If you have made a mistake in the last 4 lines, the exception raised will be different and will not be hidden by the generic

except:

block, which would also be hiding other errors, but you would believe it was wrong input from the user.

Because you are in a while loop, you can alternatively do:

try:
    print("Abbreviation is ", bearNames[userBear])
except KeyError:
    print("Something went wrong - did you not type a bear name?")
    print("beartruth: ", beartruth)
    continue  # go back to the beginning of the loop

# There was no error and continue wasn't hit, the Bear is a bear
print("Attributes are ", bearAttributes[bearNames[userBear]])
print("beartruth: ", beartruth)
beartruth = False
print("beartruth: ", beartruth)
Answer from mementum on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › while true loops vs try except
r/learnpython on Reddit: While True loops vs Try Except
May 11, 2024 -

Hey ya'll! I was wondering what peoples thoughts are on the decisive line between using While True: loops vs Try: Except: in terms of user input validation. In my small projects I've used loops to go over user input and various conditionals to check it for accuracy, and either passing it through to a variable or looping back to request a new input with some form of printed guidance.

As I have been studying, I've come across the Try: Except: conditionals, to handle those kind of errors for user input. Since I've been using the loops for so long, and with the little experience I actually have, I'm having a hard time seeing where I'd use the Try/Except method instead. The caveat being it seems cleaner and more well-written when I do it the Try/Except way.

Top answer
1 of 2
2

Since your were asking for some corrections or interpretations.

From your code

try:
    print("Abbreviation is ", bearNames[userBear])
    print("Attributes are ", bearAttributes[bearNames[userBear]])
    print("beartruth: ", beartruth)
    beartruth = False
    print("beartruth: ", beartruth)

except:
    print("Something went wrong - did you not type a bear name?")
    print("beartruth: ", beartruth)

You can be specific (and I would recommend it) with Exceptions to make sure you isolate the error that you may be expecting.

try:
    print("Abbreviation is ", bearNames[userBear])
except KeyError:
    print("Something went wrong - did you not type a bear name?")
    print("beartruth: ", beartruth)
else:
    print("Attributes are ", bearAttributes[bearNames[userBear]])
    print("beartruth: ", beartruth)
    beartruth = False
    print("beartruth: ", beartruth)

Doing it that way, you know that the Bear wasn't actually one. And only if the Bear is a real one you go into the else block to do something else.

If you have made a mistake in the last 4 lines, the exception raised will be different and will not be hidden by the generic

except:

block, which would also be hiding other errors, but you would believe it was wrong input from the user.

Because you are in a while loop, you can alternatively do:

try:
    print("Abbreviation is ", bearNames[userBear])
except KeyError:
    print("Something went wrong - did you not type a bear name?")
    print("beartruth: ", beartruth)
    continue  # go back to the beginning of the loop

# There was no error and continue wasn't hit, the Bear is a bear
print("Attributes are ", bearAttributes[bearNames[userBear]])
print("beartruth: ", beartruth)
beartruth = False
print("beartruth: ", beartruth)
2 of 2
1

The answer I eventually figured out was to put the input() inside the while. To explain...

The code as-written first asks the user for input, then begins the while. If the user enters'grizzly' the try succeeds, and bearTruth is set to false which breaks the loop. (Perhaps a break statement would serve a purpose here, but I haven't got as far as break statements yet :) )

If the user enters something which isn't a bear, that input is done, and the try begins. It fails, but we are already inside the while, and the user input is set. So the try happens again with the same value for userBear, fails again, and loops forever.

Maybe one day someone as silly as me will have this problem and find this solution.

Discussions

`while-elif-else`, `for-elif-else`, `try-except-elif-else` - Ideas - Discussions on Python.org
Generalize `elif-else` to all compound statements - #23 by xitop has suggested a bigger change including finally. However, the feedback revealed that finally should ideally be left out. So apologies for creating very similar thread, but I think clean new thread to get feedback on narrowed down ... More on discuss.python.org
🌐 discuss.python.org
1
July 2, 2024
While True loops vs Try Except
While True loops vs Try Except That's the wrong way to think about it because it's not an either/or choice. You need to use a while loop because you don't know how many tries the user will need before getting the correct input. How you decide to exit the loop is where you need to decide between using either a logical test or the try/except. More on reddit.com
🌐 r/learnpython
13
25
May 11, 2024
Try and Except... Is there a way to make except loop back to a specific point.
You need to put the while clause where you want the loop to go back to. That is, the while clause should probably be somewhere before the try. while True: # get input from user # try to convert it to a number # except when there's a problem # show an error message # restart the loop # else stop the loop More on reddit.com
🌐 r/learnpython
11
May 22, 2014
problem with try/except inside a while loop
I'm not sure why you expect this to raise exceptions. input() always returns a string. Even if it didn't, str() happily accepts floats and ints, and basically anything else. Neither of them make any distinctions as to what is considered a 'name', or whether the string is 'y' or 'n'. More on reddit.com
🌐 r/learnpython
4
1
May 5, 2019
🌐
Python.org
discuss.python.org › python help
Infinte while loop using try/except - Python Help - Discussions on Python.org
June 15, 2024 - hey y’all! I’m taking an online class called Cs50P, I thought I practiced using while loops + try/exceptions to the point of being confident using this structure. I’m running into an infinite loop when the exceptions are raised in the convert() function.
🌐
W3Schools
w3schools.com › python › python_try_except.asp
Python Try Except
Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary · Built-in Modules Random Module Requests Module Statistics Module Math Module cMath Module · Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.5rc1 documentation
Look at the following example, which asks the user for input until a valid integer has been entered, but allows the user to interrupt the program (using Control-C or whatever the operating system supports); note that a user-generated interruption is signalled by raising the KeyboardInterrupt exception. >>> while True: ...
🌐
UCI
ics.uci.edu › ~pattis › ICS-31 › lectures › tryexcept › tryexcept.txt
try/except Statements
The clause except ValueError: ... handles this exception by printing an error message. Now the try/except is finished executing, which means the block in the while True is finished executing, so the while loop takes control and executes its block (the try/except) again.
🌐
GeeksforGeeks
geeksforgeeks.org › python › try-except-else-and-finally-in-python
Try, Except, else and Finally in Python - GeeksforGeeks
The code enters the else block only if the try clause does not raise an exception. Example: Else block will execute only when no exception occurs. ... # Python code to illustrate working of try() def divide(x, y): try: # Floor Division : Gives ...
Published   July 15, 2025
Find elsewhere
🌐
Honeybadger
honeybadger.io › blog › a-guide-to-exception-handling-in-python
The ultimate guide to Python exception handling - Honeybadger Developer Blog
March 28, 2025 - Leverage the else clause on try blocks for code that should only run if no exception was raised. Good documentation or even simple comments across your codebase can go a long way toward clarifying which exceptions a function can raise. This helps your team (or even your future self) confidently write code without guesswork. It’s inevitable that you’ll eventually come across errors and exceptions when developing software applications. Monitoring and managing these Python errors effectively is crucial for maintaining the stability and performance of an application.
🌐
Sololearn
sololearn.com › en › Discuss › 2958647 › how-to-use-while-loop-for-exception-handling
How to use while loop for exception handling? | Sololearn: Learn to code for FREE!
January 9, 2022 - try: age=int(input("How old are you?: ")) except ValueError: age=int(input("Please enter a number: ")) #If user make a mistake for the second time , it will not work #I know the isdigit() method , But need some thing for floats too. ... while True: try: print( 'Enter a number: ' ) val = float( input() ) print( f'Input given {val}' ) break except ( TypeError, ValueError ): continue except EOFError: break
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-try-except
Python Try Except - GeeksforGeeks
July 23, 2025 - try: # Some Code except: # Executed if error in the # try block else: # execute if no exception finally: # Some code .....(always executed) ... # Python program to demonstrate finally # No exception Exception raised in try block try: k = 5//0 # raises divide by zero exception.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Try, except, else, finally in Python (Exception handling) | note.nkmk.me
August 15, 2023 - In Python, try and except are used to handle exceptions. Additionally, else and finally can be used to define actions to take at the end of the try-except process. 8. Errors and Exceptions - Handling ...
🌐
Python.org
discuss.python.org › ideas
`while-elif-else`, `for-elif-else`, `try-except-elif-else` - Ideas - Discussions on Python.org
July 2, 2024 - Generalize `elif-else` to all compound statements - #23 by xitop has suggested a bigger change including finally. However, the feedback revealed that finally should ideally be left out. So apologies for creating very similar thread, but I think clean new thread to get feedback on narrowed down ...
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Exceptions › intro-exceptions.html
19.1. What is an exception? — Foundations of Python Programming
After the run-time error is encountered, the python interpreter does not try to execute the rest of the code. You have to make some change in your code and rerun the whole program. ... Try to execute a block of code, the “try” clause. If the whole block of code executes without any run-time errors, just carry on with the rest of the program after the try/except statement.
🌐
Python documentation
docs.python.org › es › 3 › tutorial › errors.html
8. Errores y excepciones — documentación de Python - 3.14.4
February 22, 2026 - >>> while True: ... try: ... x = int(input("Please enter a number: ")) ... break ... except ValueError: ... print("Oops! That was no valid number.
🌐
CopyProgramming
copyprogramming.com › howto › try-except-in-a-while-loop
Python: Using Try-Except Statements within a While Loop
May 6, 2023 - While Try Except in Python 3, The code as-written first asks the user for input, then begins the while. If the user enters'grizzly' the try succeeds, and bearTruth is set to false which breaks the loop.
🌐
CopyProgramming
copyprogramming.com › howto › python-try-and-except-in-while-loop-python
Python: Implementing Try and Except in Python's While Loop
June 10, 2023 - Solution 3: When you say "while loop stops," do you mean the program exits with an error? Here is the resulting code: Solution 1: Exit the loop using an exception. Raise an exception inside the loop and catch it outside the loop. Alternatively, return from inside the loop. Solution 2: If you're not familiar with try and except functions, you could use the try block to raise an error and end the program.