I realize this has been answered but Im a super noob. I know it has to do with male/female being a string but I thought I had it defined? How can I get the calculation to work?<
Don't forget, in Python indentation is used to define a block of code. If not indented, the lines following while
are executed only once. Secondly, you will need to convert the gender
variable into the correct numerical value (depending on the user input) before calculating the BAC. Finally, I believe you want to change the calculation so that you are actually multiplying with thetime
variable.
Here's an example that demonstrates how to fix these problems. Notice that this script allows a few different options for the user input, and converts everything to lower-case before checking gender
:
gender = input('Please enter the sex male or female:').lower()
while gender not in ['m', 'f', 'male', 'female']:
print ("Error: Gender must be male or female.")
gender = input('Please enter the sex male or female:').lower()
if gender in ['male', 'm']:
gender = 0.68
else:
gender = 0.55
B = -0.015 * time * 2.84 * num / weight * gender
print ('The BAC is {0:.6f}'.format(B))
I also took the liberty of changing the formatted output a bit. Note, with Python 2.x you need to replace input
with raw_input
to automatically get the gender
value as a string.
there are several mistakes in your code. First of all, try to uphold proper indentation. For example, while
loop should look like that:
weight = int(input('Please enter weight in pounds:'))
while weight <= 0:
print ('Error: The weight cannot be negative or zero.')
weight = int(input('Please enter weight in pounds:'))
time = int(input('Please enter number of hours since first drink:'))
while time <= 0:
print ('Error: The time cannot be negative or zero.')
time = int(input('Please enter number of hours since first drink:'))
On your male or female coefficient. You haven't defined them. You just create variables male
and female
and set them to some values. What you want to do is:
gender = input('Please enter the sex male or female:')
gender_coeficients = {male: 0.68, female: 0.55}
B = -0.015 * time(2.84 * num / weight * gender_coeficients[gender])
Like that you created a dictionary with two keys, each corresponding to one gender, and paired them with coefficients' values. Those can be used by dictionary[key]
statement (gender_coeficients[gender]
in your case).
It would be also beneficial to create if-else
statement to check if a user really typed in male or female.
If you are interested how should a proper python code look like, check: https://www.python.org/dev/peps/pep-0008/ But most importantly, have fun coding.