Home > Blockchain >  Using specific string from input() to pass through conditional statements
Using specific string from input() to pass through conditional statements

Time:12-26

I am a student that has recently been introduced to python, and I am having an issue with getting this section of code to work. It always returns "Kg (or Lb) is not defined." What must I do to fix this so that the variable received from the input() can be used later in the conditional statement. Trying to solve the problem, I made a much simpler example.

x = input('What is your name? ')
if x == 'Bob':
    print('Hi')

When "Bob" is input by the user, the program returns "'Bob' is not defined."

#creating BMI calculator
weight = float(input('What is your weight? '))
units = input('Is your weight in Kg or Lb? ')
if units == 'Kg':
    height_in_metric = float(input('How many meters tall are you? Enter in decimal form.'))
    def calculate_BMI_metric():
        print('Your BMI is '   str(weight/(height_in_metric)**2))
    calculate_BMI_metric()
elif units == 'Lb':
    height_in_US = float(input('How many feet tall are you? Enter in decimal form.'))
    def calculate_BMI_USA():
        print('Your BMI is '   str((weight/2.2)/(height_in_US*.3048)**2))
    calculate_BMI_USA()

CodePudding user response:

Try reinstalling python and install 3.10, secondly, put #!/usr/bin/env python3 at the start of the script, other than here, I can't help you as I am using a Windows computer not a Macbook

CodePudding user response:

Your code has some issues, First:

#creating BMI calculator
weight = float(input('What is your weight? '))
units = input('Is your weight in Kg or Lb? ')
if units == "Kg":
    height_in_metric = float(input('How many meters tall are you? Enter in decimal form.'))
    def calculate_BMI_metric():
        weight/(height_in_metric)**2
    calculate_BMI_metric()
elif units == 'Lb':
    height_in_US = float(input('How many feet tall are you? Enter in decimal form.'))
    def calculate_BMI_USA():
        (weight/2.2)/(height_in_US*.3048)**2
    calculate_BMI_USA()

Lowercase the inputs:

if units.lower() == "Kg":
elif units.lower() == 'Lb':

Then your function doesn't return anything If you want your function to print

def calculate_BMI_metric():
            print(weight/(height_in_metric)**2)

or if you want it to return

def calculate_BMI_metric():
            return weight/(height_in_metric)**2

then print the outcome like this:

print(calculate_BMI_metric())

Leaving the fixed code

#creating BMI calculator
weight = float(input('What is your weight? '))
units = input('Is your weight in Kg or Lb? ')
if units.lower() == "Kg":
    height_in_metric = float(input('How many meters tall are you? Enter in decimal form.'))
    def calculate_BMI_metric():
        weight/(height_in_metric)**2
    print(calculate_BMI_metric())
elif units.lower() == "Lb":
    height_in_US = float(input('How many feet tall are you? Enter in decimal form.'))
    def calculate_BMI_USA():
        (weight/2.2)/(height_in_US*.3048)**2
    print(calculate_BMI_USA())

CodePudding user response:

Your code runs fine on my computer, this could be an issue with python. Can you try running it here Programmiz Online Python

  • Related