0

I am having an issue I can't seem to understand. I am having the user pass in calories/weight info through a webform.

When entering calories for the day I have the form as below, and it works just fine:

<form action="/calories" method="post">
        <fieldset>
            <div class="form-group">
                <input autocomplete="off" autofocus class="form-control" name="usercal" placeholder="Enter Calories" type="number"/>
            </div>
            <div class="form-group">
                <button class="btn btn-success" type="submit">Enter Calories</button>
            </div>
        </fieldset>
    </form>

And the python code as:

cal = int(request.form.get("usercal"))

When I have the user set goals (just entering the same number format just on a different page).

HTML:

<form action="/goals" method="post">
        <fieldset>
            <div class="form-group">
                <input autocomplete="off" autofocus class="form-control" name="userweightgoal" placeholder="Enter Goal Weight" type="number"/>
                    <button class="btn btn-primary" type="submit">Submit Goal Weight</button>
            </div>
            <div class="form-group">
                <input autocomplete="off" autofocus class="form-control" name="usercalgoal" placeholder="Enter Daily Calorie Goal" type="number"/>
                    <button class="btn btn-success" type="submit">Submit Daily Calorie Goal</button>
            </div>
        </fieldset>
    </form>

Python:

weightgoal = int(request.form.get("userweightgoal"))
calgoal = int(request.form.get("usercalgoal"))

I get the error:

ValueError: invalid literal for int() with base 10: ''

When I remove the int from the request (since the value from the form should be formatted as a number from the HTML code) I get it to pass, but the check for a positive integer fails. This is the same check I have for the calorie code that works just fine...

try:
    if weightgoal < 0:
        return apology("Enter a postitive number")
except:
    return apology("Enter a postitive number")

I have it print(weightgoal, calgoal) and it prints the number entered from the form, so I know it's being passed. It doesn't print when I have int before the form request because it fails at the int datatype. I have seen posts about passing floats etc, but these are whole numbers, so it should be passed as int...right?

What am I missing? To me theses are identical formats, but it seems to be behaving differently.

****EDITED WITH MY SOLUTION**** It's a bit verbose, but this was my workaround:

# check form and set to int variable
        weightgoalcheck = (request.form.get("userweightgoal"))
        calgoalcheck = (request.form.get("usercalgoal"))

        # if weight AND cal is "empty"
        if not weightgoalcheck:
            if not calgoalcheck:
                return apology("Must Enter Weight and/or Calorie Goal")

        # if weight goal entered
        if weightgoalcheck:
            weightgoal = int(request.form.get("userweightgoal"))

            # ensure positive number
            try:
                if weightgoal < 0:
                    return apology("Enter a positive number")
            except:
                return apology("Enter a positive number")

        # if calorie goal entered
        if calgoalcheck:
            calgoal = int(request.form.get("usercalgoal"))

            # ensure positive number
            try:
                if calgoal < 0:
                    return apology("Enter a positive number")
            except:
                return apology("Enter a positive number")
0
1

It's an either or problem. If user enters a calorie goal, then userweightgoal is '' ( notice the '' at the end of the error message), which provokes the error. And versa vice.

4
  • Thank you, that makes sense. I'm not sure how to write it then if the user only needs to input one. Maybe a conditional statement for if there is data passed through? I'm still cutting my teeth on this stuff so I'm not comfortable with it yet – Michael Syring Apr 16 '18 at 1:32
  • edited with my soluton. Is there any way to put code in the comments? – Michael Syring Apr 16 '18 at 3:15
  • You can enclose code in back-ticks in comments, which would be ok for a line or two. A comment is limited to 600 chars, it's not intended for a big code block. I wouldn't consider your solution a workaround, it's a valid solution for a conditional problem. As you work more with these languages you will find many other alternative solutions. – DinoCoderSaurus Apr 16 '18 at 13:32
  • Thanks for the positive feedback! I did what others do and just edited the question with my larger code block solution and made mention of it in the comments. – Michael Syring Apr 17 '18 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .