userchoice = raw_input('What do you want to calculate?\nmass, acceleration or force')
def calculator():
if userchoice == 'mass':
acceleration = int(raw_input('What will be the acceleration?'))
force = int(raw_input('And what it will be the force?'))
return force / acceleration
elif userchoice == 'force':
acceleration = int(raw_input('What will be the acceleration?'))
mass = int(raw_input('And what it will be the mass?'))
return mass * acceleration
elif userchoice == 'acceleration':
force = int(raw_input('And what it will be the force?'))
mass = int(raw_input('And what it will be the mass?'))
return force/mass
else:
return "You didn't choose any of the available options"
print calculator()
在我运行任何流程后,我得到0而不是操作。 采取以下流程:用户选择质量,他输入20的加速度和2的力。结果应该是10,但我得到0。
答案 0 :(得分:0)
除法输出默认转换为int。使用float()
方法,如下所示:
userchoice = raw_input('What do you want to calculate?\nmass, acceleration or force')
def calculator():
if userchoice == 'mass':
acceleration = int(raw_input('What will be the acceleration?'))
force = int(raw_input('And what it will be the force?'))
return float(force) / float(acceleration)
elif userchoice == 'force':
acceleration = int(raw_input('What will be the acceleration?'))
mass = int(raw_input('And what it will be the mass?'))
return float(mass) * float(acceleration)
elif userchoice == 'acceleration':
force = int(raw_input('And what it will be the force?'))
mass = int(raw_input('And what it will be the mass?'))
return float(force)/float(mass)
else:
return "You didn't choose any of the available options"
print calculator()
它会正常工作:
>>> ================================ RESTART ================================
>>>
>>>
What do you want to calculate?
mass, acceleration or forcemass
What will be the acceleration?3
And what it will be the force?5
1.66666666667
答案 1 :(得分:0)
我发现了问题......我想当我给予加速度20和力2时,我应该得到10,因为操作将是加速度/力,但它实际上是力/加速度......