-3

As a beginner in Python's Tkinter, I am trying to create an "Ultimate Fitness Calculator" on Tkinter for users to enter their weight, height, age and find out their BMR (basal metabolic rate) and TDEE (total daily energy expenditures). I've gotten most of the code down but I am stuck at debugging a certain part of an algorithm.

The problem is about converting a string to a float.

I'm confused because I've already written down all necessary global variables, and I thought W, H and A are already meant to be floats because I coded that way. For example, I said H = (float(Height_CM.get())), W = (float(Weight_KG.get())), and so on. Here is the bug:

line 53, in Q1d_Gender
    BMR = (66 + (13.7*float(W)) + (5*float(H)) - (6.8*float(A)))
ValueError: could not convert string to float: 'w'

Also, I tested the algorithm above by simply printing statements to the console, like this:

W = input("enter weight")
H = input("enter height")
A = input("enter age")

BMR = (66 + (13.7*float(W)) + (5*float(H)) - (6.8*float(A)))

print(BMR)

And it worked. I don't understand why it won't work with the tkinter GUI.

Check out the full code for the tkinter, and copy and paste it to Pycharm or any Python program. Thanks for helping, and yes, I am a beginner :(

Link!

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • 4
    Please don't link to code on another site. Take the time to create a [mcve]. – Bryan Oakley May 24 '18 at 13:19
  • "I thought W, H and A are already meant to be floats". But obviously at least one of them is not. What you thought is no longer relevant at this point. – Stop harming Monica May 24 '18 at 13:36
  • Please remove your `tkinter` tag as this question has nothing to do with tkinter or rather the problem you are having is not related to tkinter but purely a python problem. – Mike - SMT May 24 '18 at 13:44

2 Answers2

1

The program is trying to cast the input string into a float. Sometimes that will work ie 2 or 3.5. However the user can also input things which are not floats, like w. It will try to cast this and fail, as you are seeing.

You have to check for these non floats and do something with them. In order to determine if a string is a number, check out this question: How to check if string input is a number?

code11
  • 1,986
  • 5
  • 29
  • 37
1

You need to make sure that each value that the user inputs is a valid number. If you try to use float() on anything other than a string containing only an integer or a float then this will cause an error. If it was me I would use the try/except method here to check each value and if one of them is not a float then ask the user to re-input the values.

This can be done by using a function that check after all the answer are made. I will use a list to store all the values as it is a bit cleaner and easier to work with then multiple variables names.

See the below code.

var_list = []

def get_input(question):
    global var_list
    var_list = []
    print(question)
    var_list.append(input("enter weight: "))
    var_list.append(input("enter height: "))
    var_list.append(input("enter age: "))

    for value in var_list:
        try:
            float(value)
        except ValueError:
            get_input("Error: One of the values you entered was not a number.\nPlease answer the following questions with a number.")

get_input("Please answer the following questions with a number.")


BMR = (66 + (13.7*float(var_list[0])) + (5*float(var_list[1])) - (6.8*float(var_list[2])))

print(BMR)
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79