Why input function is not working in python

I am a python newbie.I am getting familiar with loops and tried this example from a book

while True:
        s = input['Enter something : ']
        if s == 'quit':
                break
        print['Length of the string is', len[s]]
print['Done']

However the output is as follows

Enter something : ljsdf
Traceback [most recent call last]:
  File "trial_2.py", line 2, in 
    s = input['Enter something : ']
  File "", line 1, in 
NameError: name 'ljsdf' is not defined

asked Jan 21, 2014 at 6:03

1

You have to use raw_input[] instead [Python 2.x], because input[] is equivalent to eval[raw_input[]], so it parses and evaluates your input as a valid Python expression.

while True:
        s = raw_input['Enter something : ']
        if s == 'quit':
                break
        print['Length of the string is', len[s]]
print['Done']

Note:

input[] doesn't catch user errors [e.g. if user inputs some invalid Python expression]. raw_input[] can do this, because it converts the input to a string. For futher information, read Python docs.

answered Jan 21, 2014 at 6:05

Christian TapiaChristian Tapia

33k6 gold badges53 silver badges72 bronze badges

3

you want raw_input[] in python2

while True:
    s = raw_input['Enter something : ']
    if s == 'quit':
            break
    print 'Length of the string is', len[s]
print 'Done'

input[] tries to evaluate [dangerously!] what you give it

answered Jan 21, 2014 at 6:04

mhlestermhlester

22.2k10 gold badges50 silver badges74 bronze badges

Your code will work fine in python 3.x

But if you are using python 2 you will have to input string using raw_input[]

while True:
    s = raw_input['Enter something : ']
    if s == 'quit':
        break
    print['Length of the string is', len[s]]
print['Done']

answered Jan 21, 2014 at 6:10

VipulVipul

3,9289 gold badges31 silver badges54 bronze badges

It seems like you're using Python 2.x, while the code is expected to be run in Python 3.x.

input in Python 2.x evaluates the input string unlike input in Python 3.x.

answered Jan 21, 2014 at 6:04

falsetrufalsetru

343k57 gold badges684 silver badges608 bronze badges

In Python 2.x input[] is designed to return numbers, int or float depending on the input from the user, you can also enter variable names.

you need to use:

raw_input['Enter something: ']

The error is caused because Python thinks that "ljsdf" is the name of a variable, and that's why it raises this exception:

NameError: name 'ljsdf' is not defined

becuase "ljsdf" is not defined as a variable. :D

raw_input[] is safer to use, and then convert the input to whatever other type after :D

answered Jan 21, 2014 at 6:11

You should install and use Python 3 on your Mac. Legacy Python [Python 2 - support for which ends on January 1st 2020] has raw_input[] which was renamed input[] in Python 3. Python 2 also had an input[] function but this was dropped as it evaluated and executed, as Python code, whatever a user entered even if that was an instruction to wipe all the files on the computer].

Note that Sublime is an excellent text editor but you would be better learning to use the Terminal [cmd-spacebar and type terminal]. It is possible to add a full terminal session to Sublime, but, it is a bit fiddly for a beginner and a distraction from learning Python.

You can install Python 3 by downloading and installing the official Python 3 installation package for macOS from //www.python.org/downloads/ [the site will recognise that you are using macOS].

Alternatively, you might want to explore installing it using homebrew [a package manager for macOS] using brew install python3|

Keep in mind, macOS is a Unix based operating system which is very similar to linux and most instructions you will find for linux [for distributions like Ubuntu, Debian, Fedora, CentOS, etc] will work the same on macOS - the exception being the package manager instructions as macOS does not have one as standard, hence the suggestion to install homebrew although a lot of what you will want to do will not require the use of a package manager.

How do you enable input in Python?

In Python, we can get user input like this: name = input["Enter your name: "] print["Hello", name + "!"] The code above simply prompts the user for information, and the prints out what they entered in.

How the input function works in Python?

input[] Return Value The input[] function reads a line from the input [usually from the user], converts the line into a string by removing the trailing newline, and returns it. If EOF is read, it raises an EOFError exception.

Is input [] automatically a string?

input always returns a string. It doesn't attempt to interpret anything.

How do you pass an input to a function in Python?

input[] function takes the data from the console in the form of string. If we pass multiple arguments to the input[] function, then we will get the error. Additionally, converting one type of data into other types of data is known as typecasting. We can use the functions int, str, and float for the typecasting.

Chủ Đề