3

I'm defining a function that I can call to store the user's input as a variable, make sure it's a number, and convert the number to a integer for a formula later in the code.

So the idea is, when the program gets to days it prompts the user to input getAnswer, which launches the function, to check the answer the user gives, and convert it to an integer. Same thing with worked

def getAnswer(a):
    while True:
        if a.isdecimal():
            a = int()
            break
        print('Numbers only please...')

days = input(getAnswer)
worked = input(getAnswer)

This is what it ends up looking like <function functionName at 0x013A1300>

  • 1
    It's not at all clear to me what you're trying to ask here; could you edit to clarify? Also, it appears that `getAnswer` doesn't actually return anything. – jonrsharpe Oct 14 '15 at 13:34
  • I tried to clean up the language the best I could for everyone, I'm still terribly new at Python. –  Oct 14 '15 at 15:08
  • *"follows my cursor at the input parts"*?! – jonrsharpe Oct 14 '15 at 15:09
  • Edit made, the picture is worth more than the words I was trying to put to it. =\ –  Oct 14 '15 at 15:15

2 Answers2

3

You're calling input by passing a function instead of a string prompting the user. Perhaps you mean:

days = getAnswer()

with getAnswer() defined to use input to get the response from the user, validate it, and then return it. For example,

def getAnswer():
    while True:
        a = input()
        try:
            return int(a)
        except ValueError:
            print('Numbers only please...')

In my opinion, this is rather clearer than passing the input function to getAnswer() and the try...except clause is a common and easily-understood Python idiom.

xnx
  • 24,509
  • 11
  • 70
  • 109
  • This fixed the issue, and I understand functions better, thank you! –  Oct 14 '15 at 15:33
0

getAnswer by itself, is a function. You are not calling it, for example, like this:

getAnswer(1)

I suspect what you tried to do is

days = getAnswer(input())

Note that the order of evaluation is from inside to outside. In other words, input() will be evaluated first, and then getAnswer(...)

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73