0

This is my code with lines 4-6 asking for the input file:

import numpy as np
from matplotlib import pyplot as plt

infile = input('Enter input file name:') 
mags = open( infile ,'r')
cluster = input('Enter the name of the cluster:')

vmag=[]
imag=[]

for line in mags:
    column = line.split()
    flag=int(float(column[0]))
    RA=(column[14])
    DEC=(column[15])
    RA = int(float(RA.split(':')[2]))
    DEC = int(float(DEC.split(':')[2]))

    a = input('Enter flag lower range value')
    b = input('Enter flag higher range value')    
    c = input('Enter RA lower range value:')
    d = input('Enter RA higher range value:')
    e = input('Enter DEC lower range value:')
    f = input('Enter DEC higher range value:')

    if flag in range (a,b):
    #if flag == 0:
        if RA in range (c,d):

             if DEC in range (e,f):

                vmag.append(float(column[1]))
                imag.append(float(column[4]))   

vmag = np.array(vmag)
imag = np.array(imag)


plt.scatter(vmag-imag, vmag, lw=0, s=5)
plt.ylim(27, 21)
plt.xlim(-0.5, 3.0)
plt.title('Colour Magnitude Diagram', cluster)
plt.ylabel('F555 mag')
plt.xlabel('F555-F814')
plt.grid(b=True)
plt.savefig(cluster, '.pdf')
plt.show()

But when I run it and enter the input file 'ngc185_u3kl01.txt'(which is in the same directory) I get the following error:

Enter input file name:ngc185_u3kl01
Traceback (most recent call last):

  File "<ipython-input-2-917781cbe168>", line 1, in <module>
    runfile('C:/Users/Keith/.spyder2/CMD.py', wdir='C:/Users/Keith/.spyder2')

  File "C:\Users\Keith\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 585, in runfile
    execfile(filename, namespace)

  File "C:/Users/Keith/.spyder2/CMD.py", line 5, in <module>
    infile = input('Enter input file name:')

  File "C:\Users\Keith\Anaconda\lib\site-packages\IPython\kernel\zmq\ipkernel.py", line 364, in <lambda>
    input = lambda prompt='': eval(raw_input(prompt))

  File "<string>", line 1, in <module>

NameError: name 'ngc185_u3kl01' is not defined

I have checked and double checked the name of the file matches what I type, even copied and pasted the input file name but still no success. I have also tried it with and without '.txt' on the end.

2
  • Because name 'ngc185_u3kl01' is surely not defined, but name 'ngc185_u3kl01.txt' is surely defined. I mean to say that you should input the name as ngc185_u3kl01.txt Commented Feb 13, 2015 at 10:43
  • I get the exact same error regardless of the .txt Commented Feb 13, 2015 at 10:47

2 Answers 2

1

or you can keep "input" function and use a Python 3.x interpreter.

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

Comments

1

On Python 2 you should use raw_input, not input. input evaluates the result into a number or string, and is considered dangerous and unpythonic, since you must catch the exception if the user inputs something bad, like a unquoted string or just nothing.

input is really just archaic and a cliche from the old days. That's why they changed it in Python 3.

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.