Let me preface this by saying I am a total newbie to Python programming. Sorry to bother anyone with a possibly trivial question. But I am creating a program that calculates the user's BMI and displays their info along with their calculated BMI. I must create a function for the following: 1. BMI calculation 2. Retrieve user's weight 3. Retrieve user's height 4. Display user's weight, height, and calculated BMI
Here is my code: BMI Calculator Code
These are my results: Code Results
I'm supposed to show the calculated results too, but I'm doing one step at a time, making sure I can at least print the inputs first which is not happening. I'm so confused and don't know what to do to fix it. Any feedback is appreciated. Thanks.
use return
your input in function do not return any value to w
, h
, name
in main code.
so, function need to be
def get_weight():
return int(input('message'))
return
pass value to caller, now w can get value returned by get_weight
function
w = get_weight()
You are getting the user input correctly, but you need to actually return
the user input from the function for the value to accessible outside of the function. Currently in your code you are simply getting your user input, converting it to an integer, and immediately throwing away the values by letting them be garbage collected.
To return the value from the functions, use the return
keyword:
def get_weight():
return int(input(...))
def get_height():
return int(input(...))
Along with the solution above, there are some general improvements you can make to your code:
Use newline characters, rather than extra calls to print
, for whitespace around displayed text. For example, use:
print('\\nBMI calculator\\n')
Rather than:
print() print('BMI calculator') print()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.