Input a number

0 Anantmurti Hegde · April 30, 2015
Hi, when I input a number using 
input("Enter a number : ")
the input is taken as a string but not as a number and unable to use it for further mathematical components. I'm new to python and using Python 3.4.3

Post a Reply

Replies

Oldest  Newest  Rating
0 Colonel Panic · April 30, 2015
Python 2.7.8 (default, Jul 25 2014, 14:04:36)
[GCC 4.8.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> num = input("ent: ")
ent: 4
>>> type(num)
<type 'int'>

You sure about that?

I am using a much older version of python, but I doubt that it matters, and also the version that comes standard on just about every server.   You would be hard pressed to find v3 in any production environment. 
0 Colonel Panic · April 30, 2015
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
num = input("int: ")
int:  4
=> None
type(num)
=> <class 'str'>
num = int(num)
=> None
type(num)
=> <class 'int'>

3.4.0 replicates your problem, and this is the solution
0 Colonel Panic · April 30, 2015

Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
num = int(input("Enter: "))
Enter:  4
=> None
type(num)
=> <class 'int'>


an even more "Pythonic" way of doing it
  • 1

Python

107,085 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator