我意识到这已经得到了解答,但我是一个超级菜鸟。我知道它与男性/女性是一个字符串有关但我认为我已经定义了它?如何使计算起作用?
num = int(input('Please enter number of standard drinks consumed:'))
while num <= 0:
print ('Error: The number cannot be negative or zero.')
num = int(input('Please enter number of standard drinks consumed:'))
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:'))
gender = input('Please enter the sex male or female:')
male = 0.68
female = 0.55
B = -0.015 * time(2.84 * num / weight * gender)
print ('The BAC is', format(B, ',.2f'))
答案 0 :(得分:0)
您的代码中存在多个错误。首先,尽量坚持适当的缩进。例如,while
循环应该如下所示:
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:'))
关于你的男性或女性系数。你还没有定义它们。您只需创建变量male
和female
并将其设置为某些值。你想要做的是:
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])
就像你创建了一个带有两个键的字典,每个键对应一个性别,并将它们与系数&#39;值。这些可以由dictionary[key]
语句(在您的情况下为gender_coeficients[gender]
)使用。
创建if-else
语句以检查用户是否真正键入了男性或女性也是有益的。
如果您对如何使用正确的python代码感兴趣,请检查: https://www.python.org/dev/peps/pep-0008/ 但最重要的是,请进行有趣的编码。
答案 1 :(得分:0)
while
后面的行只执行一次。其次,在计算BAC之前,您需要将gender
变量转换为正确的数值(取决于用户输入)。最后,我相信您想要更改计算,以便实际上将与time
变量相乘。
这是一个演示如何解决这些问题的示例。请注意,此脚本为用户输入提供了一些不同的选项,并在检查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))
我也冒昧地改变了格式化输出。请注意,使用Python 2.x,您需要将input
替换为raw_input
,以自动将gender
值作为字符串获取。