1

I'm trying to write a program which takes 2 inputs from the user.

  1. The location of a text file containing an array and
  2. A column in the array.

Then I want to extract that column from the array, and perform functions on it using my own module stats.

I've tried setting a variable i.e.,

def arcol(fname, i):
    data = np.loadtxt(fname, usecols=i)
    return data

in the function, but it doesn't like the apparent string usecols is set to.

Here's the code I have, the error is that the variables in the print() are not defined.

import numpy as np
import stats as st

def setInput1():
    fname = input("Please enter file location/path:")
    return fname

def setInput2():
    i = input("Please enter the desired column in the array:")
    return i

def arcol(fname, i):
    return np.loadtxt(fname, usecols= i)

print(st.mean(arcol(fname, i)))
3
  • 1
    A guess is that your input column is read in as a string, but arcol assumes it's an int. Try, i = int(i). Also, you don't show where you call the functions, so without a runable example, it's hard to say what's going on. Commented Sep 25, 2018 at 2:15
  • You never define the i or the fname you are passing in print(st.mean(arcol(fname, i))), so obviously you will get an error telling you they aren't defined Commented Sep 25, 2018 at 2:24
  • I assume setInput1 and setInput1 is called somewhere? Commented Sep 25, 2018 at 2:24

1 Answer 1

1

It looks like your issue is that you never call your setInputN functions. Just defining a function doesn't do anything useful, you have to call it to get its return value. I think you want something like this:

print(st.mean(arcol(setInput1(), setInput2())))

I've not tested your code, so there may be more errors, such as issues with the types of your input data (as written, both function will return strings, but the value you use as the second argument to arcol may need to be a number).

It would also be a good idea to pick better names for your functions. Neither of the setInputN functions have a descriptive name. arcol is maybe a little better, but it's so abbreviated that it doesn't really mean anything to me as somebody who knows nothing about what it does.

I also wonder if the setInputN functions are really needed, as they are very short and are probably not going to be called from more than one place. You could replace the calls to each function by calls to input directly.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.