I checked various questions on Stack Overflow but one thing every logic lacks. Let me demonstrate using Python:
while True:
user_input = raw_input()
if type(user_input) == str:
print 'ERROR'
else:
print 'BINGO'
Also, we cannot use input() in place of raw_input() as it gives the error:Traceback (most recent call last):
File ".\test.py", line 3, in <module>
user_input = int(input())
File "<string>", line 1, in <module>
NameError: name 'asdf' is not defined
The problem here is that raw_input converts the user input into string so it always prints 'ERROR' and if I change the second line to
user_input = int(raw_input)
then, it gives an error:
Traceback (most recent call last):
File ".\test.py", line 3, in <module>
user_input = int(raw_input())
ValueError: invalid literal for int() with base 10: 'asdf'
I tried this with try and except but it shall work fine to check integer but not a string.
raw_inputto know that the next keystrokes by the user will be an integer? Conversely, how do you expect theintfunction to cast"asdf"into an integer?