big daddy
big daddy

Reputation: 75

Function that takes arguments as input from user

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)))

Upvotes: 1

Views: 67

Answers (1)

Blckknght
Blckknght

Reputation: 104842

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.

Upvotes: 1

Related Questions