0

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?

1
  • 3
    In your first case, you appear to be running under Python 2.7. The input() function changed between version 2.7 and version 3 - see docs.python.org/2/library/functions.html#input. Commented Mar 17, 2017 at 1:58

2 Answers 2

1

Short Answer

It appears that when you run the code through Visual Studio Code, Python 2.7 is being used to run the code.

If you wish to continue using Python 2.7, use the raw_input instead of the input function.

Explanation

Take a look at the Python 2.7 documentation for the input function. It's different from the input function used in Python 3.x. In Python 2.7, the input function uses the eval function to process the input the program receives as though the input were a line of Python code.

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' 

What's happening in the case above, with Python 2.7, is:

eval("5").isdigit() # 5.isdigit()

The above Python statement is invalid, because it results in trying to call an .isdigit() method on an integer. But, integers in Python do not have that method.

malikarumi@Tetuoan2:~$ python "/home/malikarumi/Documents/PYTHON/Blaikie Python/flatnested.py"            
enter an int:  '5'
0                                                                         
1
2
3
4

In the case above, with Python 2.7, what's happening is:

eval("'5'").isdigit() # '5'.isdigit()

The statement above is valid because it results in a string calling a .isdigit() method, which does exist for strings.

I hope this answers your question and gives you a clearer understanding of the differences between the input functions in Python 2.7 and Python 3.x.

Sign up to request clarification or add additional context in comments.

1 Comment

You both gave good answers but yours was a little more detailed so I gave you the points. Thanks, I knew raw_input() had become input() going from 2 to 3, but I did not know 2 had its own input() that was different.
0

If you're using Ubuntu (or others LINUX distro), when typing python in terminal, it equals with python2, so the first time you run with python, you're using Python 2, instead of Python 3, that error is obvious. The reason why you put quote string and it worked because in Python 2, input() is equal eval(raw_input())

>>> input()
5 
5
# equal with eval(5), which is 5

With quote string

>>> input()
'5'
'5'
# equal with eval('5'), which is '5'

In second time it worked as expected since you explicitly run with python3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.