I ran this code through Visual Studio Code:
counter = 0
while True:
max_count = input('enter an int: ')
if max_count.isdigit():
break
print('sorry, try again')
max_count = int(max_count)
while counter < max_count:
print(counter)
counter = counter + 1
And was very surprised to see this response:
python "/home/malikarumi/Documents/PYTHON/Blaikie Python/flatnested.py"
malikarumi@Tetuoan2:~$ python "/home/malikarumi/Documents/PYTHON/Blaikie Python/flatnested.py"
enter an int: 5
Traceback (most recent call last):
File "/home/malikarumi/Documents/PYTHON/Blaikie Python/flatnested.py", line 7, in <module>
if max_count.isdigit():
AttributeError: 'int' object has no attribute 'isdigit'
Because input() is always supposed to return a string: https://docs.python.org/3.5/library/functions.html#input
I put in a quoted string:
malikarumi@Tetuoan2:~$ python "/home/malikarumi/Documents/PYTHON/Blaikie Python/flatnested.py"
enter an int: '5'
0
1
2
3
4
and now it works as expected. Then I ran it on my standard issue Ubuntu terminal:
malikarumi@Tetuoan2:~/Documents/PYTHON/Blaikie Python$ python3 flatnested.py
enter an int: 5
0
1
2
3
4
And it worked as expected, note no quotes around the 5.
What's going on here? Has Visual Studio Code 'rewritten' the rules of Python?
input()function changed between version 2.7 and version 3 - see docs.python.org/2/library/functions.html#input.