TypeError in calculations with user-input values

前端 未结 2 1626
余生分开走
余生分开走 2021-01-22 21:51

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?<

2条回答
  •  失恋的感觉
    2021-01-22 22:37

    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.

提交回复
热议问题