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 OverflowHey 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.
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)
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.