My Bmi calculator that I am writing for school is producing my output backwards, it first calls for user information and than for name information. Please help, i need it to be the other way around.
user = str
end = False
def bmi_intro():
print("BMI Calculator")
while end == False:
user = input("Enter students name or '0' to quit: ")
if user == "0":
print("end of report!")
else:
def userName(str):
user = str
print("Lets gather your information,", user)
break
get_height = float(input("Please enter your height in inches: "))
get_weight = float(input("Please enter your weight: "))
body_mass_index = (get_weight * 703) / (get_height ** 2)
print ("Your bmi is: ", body_mass_index)
def main():
get_height = 0.0
get_weight = 0.0
body_mass_index = 0.0
bmi_intro()
There are a number of issues in your code:
You have not set the value of end
You have not indented correctly
The main function is redundant in this case
It should be as follows:
def bmi_intro():
end = False
print("BMI Calculator")
while end == False:
user = input("Enter student's name or '0' to quit: ")
if user == "0":
print("end of report!")
end = True
else:
print("Lets gather your information,", user)
get_height = float(input("Please enter your height in inches: "))
get_weight = float(input("Please enter your weight: "))
body_mass_index = (get_weight * 703) / (get_height ** 2)
print ("Your bmi is:", body_mass_index)
bmi_intro()
Additional suggestions
You may like to indicate the unit of measurement in your question for the weight i.e.:
get_weight = float(input("Please enter your weight in pounds (lbs): "))
A function is not required unless you plan on extending this code and/or adding additional functions. You could do away with the function definition and the function call if you wish.
Correction of indentation and removing break statement can solve your problem (I have tried to edit your code as less as possible, I think it will be helpful to you to understand the code):
user = str
end = False
def bmi_intro():
print("BMI Calculator")
while end == False:
user = input("Enter students name or '0' to quit: ")
if user == "0":
print("end of report!")
break
else:
def userName(str):
user = str
print("Lets gather your information,", user)
get_height = float(input("Please enter your height in inches: "))
get_weight = float(input("Please enter your weight: "))
body_mass_index = (get_weight * 703) / (get_height ** 2)
print ("Your bmi is: ", body_mass_index)
def main():
get_height = 0.0
get_weight = 0.0
body_mass_index = 0.0
bmi_intro()
Related
I'm a Python beginner and trying to build upon my code below. I have the base working and want to collect the data from the calculator and store it in a dictionary. I created the last function to do this, but I can't pass the variables from bmi_metrics(). Why?
#Intro to BMI Calculator
print("Welcome to my BMI calculator!")
print("Give me your height and weight, I'll calculate your Body Mass Index")
# Gather BMI metrics from user and create two loops that only accept number inputs for height/weight
def bmi_metrics():
get_name = (input("\nWhat's your name? "))
while True:
try:
get_height = float(input(f"\nHi {get_name.title()}, please enter your height in inches: "))
break
except ValueError:
print("Oops, that doesn't look like a number, try again.")
while True:
try:
get_weight = float(input("Please enter your weight in pounds: "))
break
except ValueError:
print("Oops, that doesn't look like a number, try again.")
#Calculate BMI from height and weight input
BMI = (get_weight * 703) / (get_height ** 2)
#Display user BMI and weight category back to them
print(f"{get_name.title()}, your BMI is {BMI:.2f}")
if BMI <= 18.5:
print(f"A person with a BMI of {BMI:.2f} is underwieght ")
elif BMI <= 24.9:
print(f"A person with a BMI of {BMI:.2f} is normal weight ")
elif BMI <= 29.9:
print(f"A person with a BMI of {BMI:.2f} is overweight ")
else:
print(f"A person with a BMI of {BMI:.2f} is obese")
return get_name, get_height, get_weight, BMI
#Prompt user to run calculator again
def prompt_again():
while True:
run_again = input("\nWould you like to do another calculation (y/n)? ")
if run_again == 'y':
bmi_metrics()
elif run_again == 'Y':
bmi_metrics()
elif run_again == 'N':
break
elif run_again == 'n':
break
else:
print("Please enter 'y' or 'n' ")
print("Thanks for playing!")
#Collect Name/BMI data and place it in an empty dictionary
def calc_data():
calc_results = {"Name": " ", "BMI": " "}
get_name = name
bmi_metrics()
prompt_again()
Hello :) You are missing the storage of the returned values from the call at the end of the script:
get_name, get_height, get_weight, BMI = bmi_metrics()
Otherwise, the result is not stored. You have to keep in mind that all variables in python defined inside a method are local to the method unless you store them in an object, return them as result of the method or define them as global variables. If you want to use the resulting values in another function you need to pass them as arguments.
I'm writing this script for an assignment so I'd appriciate being talked through it rather than simply being handed an answer. Basically I'm trying to convert feet to meters, meters to feet, and provide a sum of the total converted distance in both at the end. Without the [] indexes, It was working perfectly. The new part I've only just added and am struggling with is the [] indexes, and to be honest I'm having a hell of a time groking how they work. Anyways heres the code:
MAX = 256
switch = ""
feet = [0.0] * MAX
meters = [0.0] * MAX
feetpermeter = 3.28084
metersperfoot = 0.3048
sum_meters = 0
sum_feet = 0
def main():
selector()
def selector():
while True:
print("Is your measurement in meters or feet?")
switch = input("Or would you like to quit?")
if (switch == "feet" or switch == "Feet"):
ftm()
elif (switch == "meters" or switch == "Meters"):
mtf()
elif (switch == "quit" or switch == "Quit"):
end()
else:
print("Sorry, that wasn't one of the options.")
print("Lets try that again")
def mtf():
try:
meters[sum_meters] = float(input("Enter the number of meters. "))
feet[sum_feet] = meters * feetpermeter
print("That is", feet, "feet.")
main()
except:
print("Sorry, I didn't quite get that, lets try again.")
mtf()
def ftm():
try:
feet[sum_feet] = float(input("Enter the number of feet. "))
meters[sum_meters] = feet * metersperfoot
print("That is", meters, "meters.")
main()
except:
print("Sorry, I didn't quite get that, lets try again.")
ftm()
def end():
while True:
switch2 = input("Are you sure you want to quit(y/n)?")
if (switch2 == "y" or switch2 == "Y"):
print("you converted a total of ", sum(feet), "feet")
print("And", sum(meters), "meters.")
print("Bye!")
exit()
elif (switch2 == "n" or switch2 == "N"):
print("Ok, let's try that again.")
main()
else:
print("Sorry, that wasn't one of the options.")
print("Lets try that again")
main()
I did try having sum_feet + 1 and sum_meters + 1 after each result but that hadnt worked either.
You are not using the indexing in a proper way. For instance , look at the comments on your existing code:
def mtf():
try:
# Error 1. You stored all the inputs to index 0, as sum_meters is 0 always and its not incremented
# So, all the inputs are not recorded, only last one gets in index 0
meters[sum_meters] = float(input("Enter the number of meters. "))
# Error 2: You multiplied the whole list against the conversion parameter.
# Instead, you should multiply the value at current index
feet[sum_feet] = meters * feetpermeter
# This will print the whole list. Again use the current index here
print("That is", feet, "feet.")
main()
except:
print("Sorry, I didn't quite get that, lets try again.")
mtf()
A fixed version of your function will be like:
def mtf():
try:
# For modifying global variables in a function scope
global sum_meters
global sum_feet
meters[sum_meters] = float(input("Enter the number of meters. "))
feet[sum_feet] = meters[sum_meters] * feetpermeter
print(f"That is {feet[sum_feet]} feet.")
sum_meters += 1
sum_feet += 1
main()
except:
print("Sorry, I didn't quite get that, lets try again.")
mtf()
This fixes stands true for your other functions as well.
I also thought to give you another piece of advice, that you can use a good object oriented approach for such problems, which makes it simpler to implement. You can learn a lot about that, then you will feel more confident.
As an example, see the below code - which does almost same, but in a more crisp way.
class Converter:
FEET_PER_METER = 3.28084
METERS_PER_FOOT = 0.3048
def __init__(self):
self.feet_store = []
self.meter_store = []
self.curr_index = 0
self.menu_handlers = {
"feet": self.feet_to_meter,
"meters": self.meter_to_feet,
"quit": self.summary
}
def run_selection(self, selected):
#
selected = str.lower(selected)
if selected in self.menu_handlers:
# call the relevant function
return self.menu_handlers.get(selected)()
return False
def meter_to_feet(self):
meters_in = float(input("Enter the number of meters."))
to_feet = meters_in * self.FEET_PER_METER
self.meter_store.append(meters_in)
self.feet_store.append(to_feet)
print(f"In Feet : {to_feet}")
return to_feet
def feet_to_meter(self):
feet_in = float(input("Enter the number of feet."))
to_meters = feet_in * self.METERS_PER_FOOT
self.feet_store.append(feet_in)
self.meter_store.append(to_meters)
print(f"In Meters : {to_meters}")
return to_meters
def summary(self):
confirm = input("Are you sure you want to quit(y/n)?")
if confirm in ["y", "Y"]:
print("you converted a total of ", sum(self.feet_store), "feet")
print("And", sum(self.meter_store), "meters.")
print("Bye!")
exit()
else:
return False
def main():
converter = Converter()
while True:
choice = input("Is your measurement in meters or feet (meters/feet/quit)?")
converter.run_selection(choice)
I hope this gives you better insights.
So theres two problems with what you've tried to do here, in the lines:
meters[sum_meters] = float(input("Enter the number of meters. "))
feet[sum_feet] = meters * feetpermeter
meters * feetpermeter is multiplying an array by a number, you need to do meters[sum_meters] to get the number you want. Secondly as you said, you need to increment sum_meters each time, but because you're inside a function you will need to declare the variable as a global before you change it. Also since sum_meters and sum_feet are always going to be equal, you can just use a single variable to keep track of this:
def mtf():
try:
global index
meters[index] = float(input("Enter the number of meters. "))
feet[index] = meters[index] * feetpermeter
index += 1
print("That is", feet, "feet.")
main()
except:
print("Sorry, I didn't quite get that, lets try again.")
mtf()
def ftm():
try:
global index
feet[index] = float(input("Enter the number of feet. "))
meters[index] = feet * metersperfoot
index += 1
print("That is", meters, "meters.")
main()
except:
print("Sorry, I didn't quite get that, lets try again.")
ftm()
I would also go a little further and say that the use of lists is unnecessary for this problem, you could simply have two numbers, total_meters and total_feet and add the values as you go. This would take less memory and also remove the arbitrary limit of 256 goes that has been imposed. So I would do:
import sys
MAX = 256
switch = ""
total_feet = 0
total_meters = 0
feetpermeter = 3.28084
metersperfoot = 0.3048
sum_meters = 0
sum_feet = 0
index = 0
def main():
selector()
def selector():
while True:
print("Is your measurement in meters or feet?")
switch = input("Or would you like to quit?")
if switch == "feet" or switch == "Feet":
ftm()
elif switch == "meters" or switch == "Meters":
mtf()
elif switch == "quit" or switch == "Quit":
end()
sys.exit(0)
else:
print("Sorry, that wasn't one of the options.")
print("Lets try that again")
def mtf():
try:
global total_feet
global total_meters
meters = float(input("Enter the number of meters. "))
feet = meters * feetpermeter
total_meters += meters
total_feet += feet
print("That is", feet, "feet.")
main()
except Exception as e:
print(e)
print("Sorry, I didn't quite get that, lets try again.")
mtf()
def ftm():
try:
global total_feet
global total_meters
feet = float(input("Enter the number of feet. "))
meters = feet * metersperfoot
total_meters += meters
total_feet += feet
print("That is", meters, "meters.")
main()
except Exception as e:
print(e)
print("Sorry, I didn't quite get that, lets try again.")
ftm()
def end():
while True:
switch2 = input("Are you sure you want to quit(y/n)?")
if switch2 == "y" or switch2 == "Y":
print("you converted a total of ", total_feet, "feet")
print("And", total_meters, "meters.")
print("Bye!")
exit()
elif switch2 == "n" or switch2 == "N":
print("Ok, let's try that again.")
main()
else:
print("Sorry, that wasn't one of the options.")
print("Lets try that again")
main()
The deposit function and the withdraw function doesn't work. After populating the accounts, I can select D or W menu options and input any number without causing the program to crash or causing an error. The program seems it is working correctly but when you check the balances using the S option, they are not updated.
Names=[]
accountnumbers=[]
balance=[]
def populatelist():
position=0
while(position<=2):
yourname= str(input("Please enter a name: "))
Names.append(yourname)
account = int( input("Please enter an account number: " ))
accountnumbers.append(account)
totalbalance = int( input("Please enter a balance: "))
balance.append(totalbalance)
position = position + 1
##################################### DEPOSIT FUCNTION
def deposit(accountnumber):
foundposition=-1
position=0
if (len(accountnumbers)>0):
while (position <=2):
if (accountnumber==accountnumbers[position]):
return position
position = position + 1
return foundposition
#################################### WITHDRAW FUNCTION
def withdraw(accountnumber):
foundposition=-1
position=0
if (len(accountnumbers)>0):
while (position <=2):
if (accountnumber==accountnumbers[position]):
return position
position = position + 1
return foundposition
def findingaccount(accountnumber):
foundposition=-1
position=0
if (len(accountnumbers)>0):
while (position <=2):
if (accountnumber==accountnumbers[position]):
return position
position = position + 1
return foundposition
def menuoptions():
print ("**** MENU OPTIONS ****")
print ("Type P to populate accounts")
print ( "Type S to search for account")
print ("Type E to exit")
print ("Type D to deposit Amount")
print ("Type W to withdraw Amount")
choice = str(input("Please enter your choice: "))
return choice
response=""
while response!= "E":
response = menuoptions()
if response=="P":
populatelist()
########################### Deposit OPTION
elif response=="D":
searchaccount = int(input("Please enter the account number to add deposit: "))
foundtheposition = deposit(searchaccount)
money = int(input("Please enter the amount to be deposited: "))
money + (balance[foundtheposition])
########################### WITHDRAW OPTION
elif response=="W":
searchaccount = int(input("Please enter the account number to withdraw: "))
thenumber = withdraw(searchaccount)
withdraw = int(input("how much for withdraw"))
withdraw - (balance[thenumber])
if (balance[thenumber]) < withdraw :
print("ERROR: Not enough balance")
elif response=="S":
searchaccount = int(input("Please enter the account number to search: "))
foundaposition = findingaccount(searchaccount)
if ( foundaposition == -1 ):
print ("The account number not found!")
else:
print ("Name is:" + str( Names[foundaposition]))
print (str(Names[foundaposition]) + " " + "account has the balance of :" +str(balance[foundaposition]))
elif response=="E":
print ("Thank you for using the program.")
print ("Bye")
exit
else:
print ("Invalid choice. Please try again!")
You have logic error.
Just change
money + (balance[foundtheposition])
to
balance[foundtheposition] = balance[foundtheposition] + money
or using short-hand operator like
balance[foundtheposition] += money
Same for
withdraw - (balance[thenumber])
Cheers ...
Your deposit scenario needs to add the amount into the selected account:
balance[thenumber] += money
Fair warning there are other errors in your program besides the accounts not updating.
I'm working on an assignment to create a code that receives input from the user on a name, weight, and height and then returns a BMI value and then loops back to ask for another name. I cannot seem to get my BMI function to return an output though.
def BMI(BMI):
num1, num2 = weight, height
BMI = (num1 * 706)/(num2^2)
return BMI
user = str
end = "x"
while user != end:
print()
user = input("Please enter player name or type 'X' to quit: ")
if user == end:
print("Report Complete")
break
else:
num1 = (float(input("Please enter weight: ")))
num2 = (float(input("Please enter height: ")))
if num1 >= 1:
print("BMI: "(BMI))
i receive this error: "" when I run the function.
I'm looking for an input that is along the lines of
Please enter player name or press 'X' to quit: xxx
Please enter weight: xxx
Please enter height: xxx
BMI: xxx
Please enter player name or press 'X' to quit:
Okay, so there are a few things that you'll want to fix for this to work.
When you are printing, you are not calling the function correctly and python thinks you're trying to use "BMI: " as a function. How you want to call BMI is like BMI(weight, height) where you pass in the values for weight and height as parameters. BMI returns a float, so you'll need to turn it into a string before appending it. The result of those changes are "BMI: "+ str(BMI(num1, num2))
In the definition of your BMI function, you'll want to pass in the weight and height as parameters like so: def BMI(weight, height):. This will allow you to use both weight and height in the function body.
The ^ in python does not mean power, it means bitwise xor. You want to use the ** operator like so: BMI = (num1 * 706)/(num2**2)
After making those changes, you should end up with something like this:
def BMI(weight, height):
num1, num2 = weight, height
BMI = (num1 * 706.0)/(num2**2)
return BMI
user = str
end = "x"
while user != end:
print()
user = input("Please enter player name or type 'X' to quit: ")
if user == end:
print("Report Complete")
break
else:
num1 = (float(input("Please enter weight: ")))
num2 = (float(input("Please enter height: ")))
if num1 >= 1:
print("BMI: " + str(BMI(num1, num2)))
I'm working on a homework assignment for my game dev. class and I can't quite figure out what I'm doing wrong. I am trying to add numbers to a high score that has already been hardwired in by the use of user input. For example; Trying to add 100 points to Roger who currently has 3456 points. I just haven't quite figured out what I'm doing wrong to get the code to work. All and any help is very appreciated. Thank you.
str1 = ["Roger", 3456]
str2 = ["Justin", 2320]
str3 = ["Beth", 1422]
enter = 0
start = 0
Roger = ()
def incdec(sco):
return addit
def addition(num1):
return num1 + num1
def square(num):
print("I'm in square")
return num * num
def display(message):
"""Display game instuctions"""
print(message)
def instructions():
"""Display game instuctions"""
print("Welcome to the world's greatest game")
def main():
instructions()
scores = [str1, str2, str3]
start = input("Would you like to view the high score options? y/n ")
if start == "y":
print("""\
Hello! Welcome to the high scores!
Here are the current high score leaders!:
""")
print(scores)
print("""\n\
0 - Sort high scores
1 - Add high score
2 - Reverse the order
3 - Remove a score
4 - Square a number
5 - Add 2 numbers together
6 - Add to a score
7 - Subtract from a score
""")
option = int(input("Please enter your selection "))
while option < 8:
print(scores)
if option == 0:
scores.sort()
print("These are the scores sorted alphabetically")
print(scores)
option = option = int(input("Please enter your selection"))
elif option == 1:
print(scores)
print("Please enter your name and score; After entering your name, hit the return key and enter your score")
name = input()
score = int(input())
entry = (name,score)
scores.append(entry)
print(scores)
option = option = int(input("Please enter your selection"))
elif option == 2:
print(scores)
scores.reverse()
print("\nHere are the scores reversed")
print(scores)
option = option = int(input("Please enter your selection"))
elif option == 3:
print(scores)
print("Please enter the high score you would like to remove. After typing the name, hit the return key and enter the score")
name1 = input()
score1 = int(input())
remove = (name1,score1)
scores.remove(remove)
print(scores)
option = option = int(input("Please enter your selection"))
elif option == 4:
val = int(input("Give me a number to square"))
sqd = square(val)
print(sqd)
option = option = int(input("Please enter your selection"))
elif option == 5:
val0 = int(input("Give me one number"))
val1 = int(input("Give me another number"))
addi = (val0 + val1)
print(addi)
option = option = int(input("Please enter your selection"))
elif option == 6:
sc0 = input("Please enter the person whose score you would like to add to ")
sc1 = int(input("Please enter the amount you would like to add to their score "))
addit =(sc0 + str(sc1))
print(addit)
option = option = int(input("Please enter your selection"))
elif option == 7:
break
main()
You're adding a number to a string. What you need to do is search for the player in your scores list and then add the given number to their current score. Using your current structure, you could do the following:
def update_score(scores, person, amount):
# Loop through the current scores
for score in scores:
# Find the person we're looking for
if score[0].lower() == person.lower():
# Update their score
score[1] += amount
return scores
And then modify as such:
...
elif option == 6:
sc0 = input("Please enter the person whose score you would like to add to ")
sc1 = int(input("Please enter the amount you would like to add to their score "))
scores = update_score(scores, sc0, sc1)
print(scores)
option = option = int(input("Please enter your selection"))
...
Which produced for me:
Please enter the person whose score you would like to add to roger
Please enter the amount you would like to add to their score 100
[['Roger', 3556], ['Justin', 2320], ['Beth', 1422]]
However, I would refactor a lot of the boilerplate away and use dictionary lookups for tracking players. But the above solution solves your stated problem.