Need help for a school project (BMI- calculator)

This is the place for queries that don't fit in any of the other categories.

Need help for a school project (BMI- calculator)

Postby eXodus » Sun Mar 03, 2013 9:48 am

Hey,
for school we have to write a Body Mass Index calculator with python.
the calculation works fine. But we have to program that depending on the BMI, in a label should emit from "Very severely underweight" up to "Obese Class III" (from this table on wikipedia: http://en.wikipedia.org/wiki/Body_mass_index).

I tried it like this, but i doesn´t work:

Code: Select all
if float(anzeigeBMI) <= 16:
        labelAuswertung2.config(text="Starkes Untergewicht", background="#2E2EFE")
    elif float(anzeigeBMI) > 16:
        labelAuswertung2.config(text="maeßiges Untergewicht", background="#0000FF")
    elif float(anzeigeBMI) > 17:
        labelAuswertung2.config(text="leichtes Untergewicht", background="#A9BCF5")
    elif float(anzeigeBMI) > 18:
        labelAuswertung2.config(text="Normalgewicht", background="#088A08")
    elif float(anzeigeBMI) > 25:
        labelAuswertung2.config(text="Praedipositas/ Uebergewicht", background="#FFFF00")
    elif float(anzeigeBMI) > 30:
         labelAuswertung2.config(text="Adipositas Grad I", background="#FF8000")
    elif float(anzeigeBMI) >35:
         labelAuswertung2.config(text="Adipositas Grad II", background="#DF0101")
    elif float(anzeigeBMI) >= 40:
         labelAuswertung2.config(text="Adipositas Grad III", background="#610B0B")


the variable "anzeigeBMI" is the BMi which is calculated ot ouf the inputs.
The "labelAuswertung2" is the label where the evaluation of the BMI should stand.
And the texts are on german, but i think it doesn´t matter here.. :>

I hope someone can help me :)
eXodus
 
Posts: 3
Joined: Sun Mar 03, 2013 9:34 am

Re: Need help for a school project (BMI- calculator)

Postby metulburr » Sun Mar 03, 2013 9:55 am

Please include a traceback as indicated here
There is no way i can tell what is wrong just from an if condition without more code and traceback.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Need help for a school project (BMI- calculator)

Postby eXodus » Sun Mar 03, 2013 10:01 am

Code: Select all
from tkinter import *

def bmiberechnung(Groesse, Gewicht):
    BMIm = (Gewicht/(Groesse*Groesse))
    return (BMIm)

def Button_Berechnen_Clickm():
   # Uebernahme der Daten
        Gewicht = float(entryGewicht.get())
    Groesse = float(entryGroesse.get())
    # Verarbeitung der Daten
    BMI = bmiberechnung(Groesse, Gewicht)
    # Anzeige der Daten
    anzeigeBMI = '{0:.2f}'. format(BMI)
    labelWertBMI.config(text=anzeigeBMI)
    if  entryGeschlecht == "maennlich":
        frameEingaben.config(background="#0000FF")
    elif entryGeschlecht == "weiblich":
        frameEingaben.config(background="#FF00BF")

    if float(anzeigeBMI) <= 16:
        labelAuswertung2.config(text="Starkes Untergewicht", background="#2E2EFE")
    elif float(anzeigeBMI) > 16:
        labelAuswertung2.config(text="maeßiges Untergewicht", background="#0000FF")
    elif float(anzeigeBMI) > 17:
        labelAuswertung2.config(text="leichtes Untergewicht", background="#A9BCF5")
    elif float(anzeigeBMI) > 18:
        labelAuswertung2.config(text="Normalgewicht", background="#088A08")
    elif float(anzeigeBMI) > 25:
        labelAuswertung2.config(text="Präadipositas/ Ãœbergewicht", background="#FFFF00")
    elif float(anzeigeBMI) > 30:
         labelAuswertung2.config(text="Adipositas Grad I", background="#FF8000")
    elif float(anzeigeBMI) >35:
         labelAuswertung2.config(text="Adipositas Grad II", background="#DF0101")
    elif float(anzeigeBMI) >= 40:
         labelAuswertung2.config(text="Adipositas Grad III", background="#610B0B")







# Erzeugung des Fensters
tkFenster = Tk()
tkFenster.title("BMI- Rechner")
tkFenster.geometry('700x500')
# Rahmen fuer die Ueberschrift
frameUeberschrift = Frame(master=tkFenster,
                          background="#889E9D")
frameUeberschrift.place(x=5, y=5, width=690, height=45)
# Rahmen fuer die Eingaben
frameEingaben = Frame(master=tkFenster,
                      background="#ACFA58")
frameEingaben.place(x=5, y=60, width=690, height=100)

# Rahmen fuer die Verarbeitung
frameVerarbeitung = Frame(master=tkFenster,
                          background="#FBD975")
frameVerarbeitung.place(x=5, y=172, width=690, height=44)
# Rahmen fuer die Ausgaben
frameAusgabe = Frame(master=tkFenster,
                      background="#ACFA58")
frameAusgabe.place(x=5, y=221, width=690, height=101)
# Rahmen fuer die Auswertung
frameAuswertung = Frame(master=tkFenster,
                       background="#ACFA58")
frameAuswertung.place(x=5, y=325, width=690, height=168)
# Label fuer die Ueberschrift
labelUeberschrift = Label(master=frameUeberschrift,
                          background="white",
                          text="BMI- Rechner")
labelUeberschrift.place(x=270, y=12, width=190, height=20)
# Label mit Aufschrift Gewicht
labelgewicht = Label(master=frameEingaben,
                             background="white",
                             text="Gewicht in [kg]")
labelgewicht.place(x=385, y=10, width=175, height=20)
# Label mit Aufschrift Alter
labelalter = Label(master=frameEingaben,
                             background="white",
                             text="Alter in [Jahren]")
labelalter.place(x=5, y=10, width=175, height=20)
# Label mit Aufschrift Groesse
labelgroesse = Label(master=frameEingaben,
                             background="white",
                             text="Groesse in [m]")
labelgroesse.place(x=385, y=60, width=175, height=20)
# Label mit Aufschrift Vorname
labelvorname = Label(master=frameEingaben,
                             background="white",
                             text="Dein Name")
labelvorname.place(x=5, y=35, width=175, height=20)
# Label mit Aufschrift name
labelname = Label(master=frameEingaben,
                             background="white",
                             text="Dein Nachname")
labelname.place(x=385, y=35, width=175, height=20)
# Label mit Aufschrift Geschlecht
labelGeschlecht = Label(master=frameEingaben,
                        background="white",
                        text="Dein Geschlecht")
labelGeschlecht.place(x=5, y=60, width=175, height=20)
# Entry fuer das Alter
entryAlter = Entry(master=frameEingaben)
entryAlter.place(x=210, y=10, width=80, height=20)
# Entry fuer das Gewicht
entryGewicht = Entry(master=frameEingaben)
entryGewicht.place(x=580, y=10, width=80, height=20)
# Entry fuer den Vornamen
entryVorname = Entry(master=frameEingaben)
entryVorname.place(x=210, y=35, width=80, height=20)
# Entry fuer den Namen
entryName = Entry(master=frameEingaben)
entryName.place(x=580, y=35, width=80, height=20)
# Entry fuer die Groesse
entryGroesse = Entry(master=frameEingaben)
entryGroesse.place(x=580, y=60, width=80, height=20)
# Entry fuer das Geschlecht
entryGeschlecht = Entry(master=frameEingaben)
entryGeschlecht.place(x=210, y=60, width=80, height=20)
# Button Berechnen m
buttonBerechnenm = Button(master=frameVerarbeitung,
                         text="berechnen",
                         command=Button_Berechnen_Clickm)
buttonBerechnenm.place(x=290, y=12, width=100, height=20)
# Label mit Aufschrift Dein BMI
labelBMI = Label(master=frameAusgabe,
                          background="white",
                          text="Dein BMI ist:")
labelBMI.place(x=130, y=30, width=175, height=20)
# Label fuer den Wert des BMI
labelWertBMI = Label(master=frameAusgabe,
                              background="white",
                              text="")
labelWertBMI.place(x=330, y=30, width=100, height=20)
# Label fuer Auswertung
labelAuswertung = Label(master=frameAuswertung,
                  background="white",
                   text="Auswertung:")
labelAuswertung.place(x=310, y=60, width=100, height=20)
# Label fuer Auswertung
labelAuswertung2 = Label(master=frameAuswertung,
                  background="white",
                   text="")
labelAuswertung2.place(x=310, y=90, width=200, height=20)


# Aktivierung des Fensters
tkFenster.mainloop()


This is my whole code. and there is no traceback. the program works, but it doesn´t give the right evaluation out..
eXodus
 
Posts: 3
Joined: Sun Mar 03, 2013 9:34 am

Re: Need help for a school project (BMI- calculator)

Postby metulburr » Sun Mar 03, 2013 10:15 am

It is actually hard to tell with everthing being in another language. If you are not getting the expected results, check your math. I am getting the traceback error:
Code: Select all
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__
    return self.func(*args)
  File "forum2.py", line 11, in Button_Berechnen_Clickm
    Groesse = float(entryGroesse.get())
ValueError: could not convert string to float:

upon executing "berechnen", with the language converted from google translate. I dont do well with other languages, maybe someone else here would find the process easier as they might know german.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Need help for a school project (BMI- calculator)

Postby eXodus » Sun Mar 03, 2013 10:30 am

it doesn´t matter. i found "the mistake".

In these if- queries i only change "elif" to "if" and now it´s working fine :>
eXodus
 
Posts: 3
Joined: Sun Mar 03, 2013 9:34 am

Re: Need help for a school project (BMI- calculator)

Postby stranac » Sun Mar 03, 2013 10:47 am

metulburr wrote:I am getting the traceback error:

Yeah, it expects you to enter data before calculating.

eXodus:
Your code has a logical error:
You first check if a number is greater than 16, than if it's greater than 17 etc.
But if it's greater than 17, it will also be greater than 16, so that's the first thing that's going to match.

There are a few ways to fix this.
You could explicitly check if the BMI is between two values using something like:
Code: Select all
if 16 <= BMI < 17:

Or you could reverse the order of your if/elif statements, to first check for higher values.

And as metulburr noted, it might be easier to get help in the future if you translate your code to english, as code in other languages is hard to read to most people.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1212
Joined: Thu Feb 07, 2013 3:42 pm

Re: Need help for a school project (BMI- calculator)

Postby metulburr » Sun Mar 03, 2013 12:16 pm

Yeah, it expects you to enter data before calculating.

That was after entering data, and calculating.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 6 guests