Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
so, what if you are trying to check someone's input if the input is a string or not. BUT whenever you catch an exception in the statement the value of the variable gets deleted. and you can't print it anymore. how can you catch a exception and still print the VALUE of the input.
i hope you got what i said


Python
try:
    someValue =int(input('> '))
except ValueError:
    print(someValue)


[Edit]

The error is:
Python
NameError: name 'someValue' is not defined


What I have tried:

trying sys.stdin.read, sys.stdin.readline
and many other things
Posted
Updated 2-Aug-17 2:02am
v3

1 solution

You can't use someValue in the except statement, because it would never be created if you enter the except block. someValue would contain the parsed integer, but if you can't do the parsing, the variable assignment will never happen. So "the variable gets deleted" is false, "the variable never gets created" would be a correct statement.

Try this instead:
Python
user_input = input('> ')
try:
    some_value = int(user_input)
except ValueError:
    print(user_input)
 
Share this answer
 
Comments
Member 13341498 2-Aug-17 9:38am    
oh thank you very much for this... i literally spent many hours trying to fix this..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900