functions and indentation in python [duplicate]

2024/11/13 3:35:26

I'm taking a tutorial in udemy to teach myself Python and it is making me love Javascript every day more. Anyhow, I can't seem to figure out how to work this indentation right so that the program runs inside a while loop that evaluates a zero string to exit BUT all input and calculations must be inside functions.

I've got this far, but the weight, height, and bmi variables are throwing undefined errors. Problem is, as far as I know, didn't I define them already in their functions?

NameError: name 'weight' is not defined

So, I thought, maybe it's because I need to indent the function calls to give the variable declarations a higher scope and what do you know! the evil red squiggly underline went away. So I ran the program with no errors and but nothing ran inside the while loop. it ignored everything! ugh....

Any help please. God, I love squiggly brackets and semicolons.

def BMICalculator():student_name = input("Enter the student's name or 0 to exit:")while student_name != "0":def getWeight():weight = float(input("Enter " + student_name + "'s weight in pounds: "))return weightdef getHeight():height = float(input("Enter " + student_name + "'s height in pounds: "))return heightdef calcBMI(weight, height):bmi = (weight * 703) / (height * height)return bmigetWeight()getHeight()calcBMI(weight, height)print("\n" + student_name + "'s BMI profile:")print("---------------")print("Height:", height, "\nWeight:", weight)def showBMI(bmi):print("BMI Index:", "{:.2f}".format(bmi))showBMI(bmi)student_name = input("Enter next student's name or 0 to exit:")print("\n\nExiting Program!")BMICalculator()
Answer

didn't I define them already in their functions

Yes, and that's the problem. They are locally scoped to those functions. You need assignment... weight = getWeight(), for example


No need to go overboard with the function definitions, though

def calcBMI(weight, height):bmi = (weight * 703) / (height * height)return bmidef showBMI(bmi):print("BMI Index:", "{:.2f}".format(bmi))student_name = input("Enter the student's name or 0 to exit:")
while student_name != "0":weight = float(input("Enter " + student_name + "'s weight in pounds: "))height = float(input("Enter " + student_name + "'s height in pounds: "))bmi = calcBMI(weight, height)# etc...showBMI(bmi)student_name = input("Enter next student's name or 0 to exit:")print("\n\nExiting Program!")
https://en.xdnf.cn/q/120513.html

Related Q&A

How do I define a method to store a collection (e.g., dictionary)?

Im a beginner working on a library management system and theres something I cant get my head around. So I have a Books class that creates new book records. class Books:def __init__(self, title=None, au…

Python Regex punctuation recognition

I am stumped by this one. I am just learning regular expressions and cannot figure out why this will not return punctuation marks.here is a piece of the text file the regex is parsing:APRIL/NNP is/VBZ …

cant find brokenaxes module

I want to create a histogram with broken axes and found that there must be a module doing this called brokenaxes that works together with matplotlib (source) . Anyway, when I trie to import the modul…

Python-Pandas While loop

I am having some trouble with the DataFrame and while loop:A B 5 10 5 10 10 5I am trying to have a while loop that:while (Column A < Column B):Column A = Column A + (Column B / 2)Colu…

split an enumerated text list into multiple columns

I have a dataframe column which is a an enumerated list of items and Im trying to split them into multiple columns. for example dataframe column that looks like this:ID ITEMSID_1 1. Fruit 12 oranges 2…

Python using self as an argument [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

how to shuffle questions in python

please anyone help me i have posted my whole work down can anyone tell me how to shuffle theses five questions it please i will be very thankful.print("welcome to the quiz") Validation = Fals…

Selenium python do test every 10 seconds

I am using selenium (python) testing and I need to test my application automatically every 10 seconds.How can I do this?

Type Object has no attribute

I am working on a program, but I am getting the error "Type object Card has no attribute fileName. Ive looked for answers to this, but none that Ive seen is in a similar case to this.class Card: R…

Generate random non repeating samples from an array of numbers

I made a battleships game and I now need to make sure that the computer doesnt attack at the same spot twice.My idea of it is storing each shots co-ordinates in a variable which gets added to whenever …