Reputation: 195
When running the following code:
try:
key=int(input())
except ValueError as string:
print("Error is within:",string)
for example, if one puts 'rrr' this exception will rise, since 'rrr' does not support (int)
However, instead ot putting the actual string, it puts: "invalid literal for int() with base 10: 'rrr' "
How do I make it work so that the variable 'string' actually gets the wrong input that the user gave (in this example, I want it to print: 'Error is within: rrr')
Thanks a lot
Upvotes: 0
Views: 1541
Reputation: 12772
You can just parse the error msg :D
print("Error is within:", string.args[0][41:-1])
Upvotes: 0
Reputation: 2068
Your issue comes from the fact that the variable string
is the error message for a ValueError exception. If you wanted to print out the user's invalid input, you would need to create a variable that stores the user's input before your try/except. For example:
userInput = input()
try:
key=int(userInput)
except ValueError:
print("Error is within:",userInput)
Upvotes: 5
Reputation: 48546
Store the input and convert it to an int separately.
key_str = input()
try:
key = int(key_str)
except ValueError:
print("Error is within:", key_str)
Upvotes: 5