TypeError in calculations with user-input values

前端 未结 2 1627
余生分开走
余生分开走 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:30

    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.

提交回复
热议问题