0

Script:

#!/usr/bin/python
for i in range(5):
    value = input ("Enter the value : ")
    print value

While string as input:

$ ./fun+for.py 
Enter the value : John
Traceback (most recent call last):
  File "./fun+for.py", line 13, in <module>
    value = input ("Enter the value : ")
  File "<string>", line 1, in <module>
NameError: name 'John' is not defined

While integer input:

$ ./fun+for.py 
Enter the value : 4
4
Enter the value : 5
5
Enter the value : 6
6
Enter the value : 7
7
Enter the value : 7
7
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Sreejith
  • 434
  • 3
  • 19
  • I know it's in python2.7, but the issue is not with printing, it is with assigning a string to "value", The string entered in quotes are accepted. But i want to input strings without quotes. – Sreejith Mar 29 '18 at 11:51

2 Answers2

2

Because in Python2, the string you enter really goes through an eval(), so it searches John as a variable name (try entering 2+1). Use raw_input() instead of input() (in python3, input() no longer does this).

xenoid
  • 8,396
  • 3
  • 23
  • 49
-2

This is because it is python 2; it will work if you copy and paste this code in python 3 -- it's better, I think. you will be able to input strings, integers, and doubles without receiving an error.

for i in range(5):
    value = input ("Enter the value : ")
    print (value)
  • You need parenthesis when you print value for python3.

..hope this helps.

lyxαl
  • 1,108
  • 1
  • 16
  • 26
Code1337
  • 1
  • 3