【问题标题】:invalid syntax, if statement. if time < 0无效的语法,if 语句。如果时间 < 0
【发布时间】:2018-02-22 02:05:19
【问题描述】:
def main():
    get_variables()

def get_variables():
    initial_position = float(input("Enter the object's initial position: "))

    initial_velocity = float(input("Enter the object's initial velocity: "))

    acceleration = float(input("Enter the object's acceleration: "))

    time = float(input("Enter the time that has passed: "))
    if time < 0
        print('The time cannot be negative')
        time = float(input('Enter a valid time: ')

    #formula
    position_of_object = initial_position + initial_velocity * time + 0.5 * acceleration * time ** 2

    #Display Position of object
    print("The position of the object is: ", position_of_object)



    while keep_going == 'y'
        keep_going = input("Do you want to calculate another Object's poistion?" + \
                       'position_of_object (Enter y for yes): ') 

main()

这是我的作业,上面写着

"如果时间

我对编程几乎一无所知,并且正在参加有关它的在线课程。请告诉我哪里错了

【问题讨论】:

    标签: python syntax


    【解决方案1】:

    您的代码中似乎很少有错误:

    1:缺少“:”和“)”:

        if time < 0
            print('The time cannot be negative')
            time = float(input('Enter a valid time: ')
    

    应改为:

        if time < 0:
            print('The time cannot be negative')
            time = float(input('Enter a valid time: '))
    

    2:在 wile 循环中缺少“:”:

        while keep_going == 'y'
            keep_going = input("Do you want to calculate another Object's 
                          poistion?" + \
                          'position_of_object (Enter y for yes): ')
    

    应该改成

        while keep_going == 'y':
                  keep_going = input("Do you want to calculate another Object's 
                        poistion?" + \
                       'position_of_object (Enter y for yes): ')
    

    【讨论】:

      猜你喜欢
      • 2012-01-31
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      相关资源
      最近更新 更多