我正在我的班级做卡车制动模型,它在编译时不会编译或返回摘要。我做错了什么?

时间:2015-12-13 18:59:06

标签: python physics

我希望有人看看我的代码。我在我的代码中包含了很多注释,所以希望它易于阅读和理解。输入(按顺序):

0.5
50
-28
0.05

错误讯息:

Traceback (most recent call last):
  File "C:\jasmin\Jasmin\python\School Projects\Braking Model.py", line 59, in <module>
    truck_braking(react_time, init_vel, break_acc, user_chg_in_time)
  File "C:\jasmin\Jasmin\python\School Projects\Braking Model.py", line 42, in truck_braking
    print((int(time) - user_chg_in_time) * 100) / 100, '          ', math.ceil(((int(disp)) * 100) / 100), '          ', math.ceil(((int(speed)) * 100) / 100)
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

该程序使用Euler的方法,应该是非程序员可以轻松使用的东西。

这是:

#import math to use ceiling function
import math

#defined function

def truck_braking(react_time, init_vel, break_acc, user_chg_in_time):


    #assign variables to be referenced later
    chg_in_time = 0
    speed = init_vel
    disp = 0
    orig_disp = 0
    time = 0
    original_inputs = [react_time, init_vel, break_acc, user_chg_in_time]

    #prints table headings
    print('Time  Displacement  Speed/Velocity')

    #calculate outputs until truck stops
    while speed > 0:

    #calculate speed
    if time >= react_time:
        if break_acc < 0:
            speed = (speed + break_acc) * user_chg_in_time
        elif break_acc > 0:
            speed = (speed - break_acc) * user_chg_in_time
    else:
        speed = init_vel

    #calculate displacement
    disp += speed * time

    #change dt value
    chg_in_time = user_chg_in_time

    #adjust time value for next calculation/loop
    time += user_chg_in_time

    #creates table of values
    print((int(time) - user_chg_in_time) * 100) / 100, '          ', math.ceil(((int(disp)) * 100) / 100), '          ', math.ceil(((int(speed)) * 100) / 100)

    #arranges final outputs in list
    final_outputs = [(time - user_chg_in_time) * 100 / 100, math.ceil(((disp) * 100) / 100), math.ceil(((speed) * 100) / 100)]

    #returns summary # for stack overflow helper: this didn't work, it compiled but summary didn't show up
    return('The original inputs for reaction time, initial speed, breaking acceleration/decceleration, and change in time were ', original_inputs, '. The final outputs for time, displacement(distance), and speed respectively were ', final_outputs, '.')


#loops for as long as wants without having to run again
willingness = 'yes'
while willingness == 'yes':
    willingness = 'Would you like to do a calculation? Type "yes" for yes, and any other key for no.'
react_time = float(input('What is the reaction time?'))
init_vel = float(input('What was the initial velocity? (Speed at time = 0)'))
break_acc = float(input('What was the breaking speed of the truck? Can be positive or negative.'))
user_chg_in_time = float(input("What would you like the change in time to be? With the way Euler's method works, which is the method of calculation this program uses, the smaller this value is, the more accurate the calculations."))
truck_braking(react_time, init_vel, break_acc, user_chg_in_time)

1 个答案:

答案 0 :(得分:1)

问题在于这个完全不可读的界限:

print((int(time) - user_chg_in_time) * 100) / 100, '          ', math.ceil(((int(disp)) * 100) / 100), '          ', math.ceil(((int(speed)) * 100) / 100)

您的括号已关闭,并且您尝试将print的返回值转移到None100

重新排列代码:

value1 = ((int(time) - user_chg_in_time) * 100) / 100
value2 = math.ceil(((int(disp)) * 100) / 100)
value3 = math.ceil(((int(speed)) * 100) / 100)

并使用字符串格式化输出:

print('{:15.2f} {:15.2f} {:15.2f}'.format(value1, value2, value3))

这是使用Python的string formatting capabilities

例如。这样:

>>> '{:7.3f}'.format(3.5657676)
'  3.566'

格式化一个总宽度为7和3位小数的浮点数。使用多个{} {}允许具有多个值。 format提供了更多内容,因此这种格式称为Formatting Mini Language

在此更改后,它会以我的输入运行。