model 14
Posts: 9
Joined: Thu Aug 22, 2019 11:52 am

Python in Raspbian Lite treats input() as a number.

Wed Sep 11, 2019 10:16 pm

I have been learning Python on my Windows computer (V 3.7.4). I just started working with Python on my new RPi 4B (V 2.7.16) using Raspbian Lite. The windows version treats a variable input from input() as a string, the version on my Pi treats it as a number. I ran the following code on each version:

n = input() I input 3
m = input() I input 4
print(n + m)

I got 34 from the version on windows and 7 on the Pi.
I would appreciate some help.
Thanks.

User avatar
joelostinspace
Posts: 7
Joined: Sat Aug 10, 2019 2:51 pm
Location: Earth

Re: Python in Raspbian Lite treats input() as a number.

Wed Sep 11, 2019 10:52 pm

what do do you get in idle? How do convert types?

Code: Select all

>>> n= input()
3
>>> n
'3'
>>> type(n)

I would rather live in a world where my life is surrounded by mystery than a world so small that my mind could comprehend it.


--- Harry Emerson Fosdick

model 14
Posts: 9
Joined: Thu Aug 22, 2019 11:52 am

Re: Python in Raspbian Lite treats input() as a number.

Wed Sep 11, 2019 11:43 pm

I use Idle on my Windows computer. My results are as you show. n is a str.
For the Pi I get: >>>> n 3, type(n) 'int'

Crazy, huh?

Andyroo
Posts: 4134
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Python in Raspbian Lite treats input() as a number.

Wed Sep 11, 2019 11:57 pm

Play safe and define the variable first by setting it to 0 before use?
Need Pi spray - these things are breeding in my house...

pcmanbob
Posts: 6619
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python in Raspbian Lite treats input() as a number.

Thu Sep 12, 2019 7:49 am

The reason for the difference between python 2 and python 3 is that,

python 2 input() evaluates the input in the execution context in which it is called, so if you enter a number its treated as a number.

python 3 input() was removed and you now only have raw_input() that has been renamed as input(), so now every input is treated as a string.

https://sebastianraschka.com/Articles/2 ... -via-input
Last edited by pcmanbob on Thu Sep 12, 2019 12:34 pm, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

hippy
Posts: 5757
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Python in Raspbian Lite treats input() as a number.

Thu Sep 12, 2019 10:05 am

model 14 wrote:
Wed Sep 11, 2019 10:16 pm
I have been learning Python on my Windows computer (V 3.7.4). I just started working with Python on my new RPi 4B (V 2.7.16) using Raspbian Lite.
The simple answer is to use Python 3 on the Pi, then your code will work the same on both.

Use "python3 yourfile.py" rather than "python yourfile.py".

User avatar
bensimmo
Posts: 4152
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Python in Raspbian Lite treats input() as a number.

Thu Sep 12, 2019 11:37 am

As above, use Python3.
You fallen on the very very common confusion with having to type python3.

Though take what you have learned with you. If/when you come across Python2 only code.

model 14
Posts: 9
Joined: Thu Aug 22, 2019 11:52 am

Re: Python in Raspbian Lite treats input() as a number.

Thu Sep 12, 2019 12:18 pm

Thanks for the help. I am now using Python3.

Return to “Python”