How do I make this simple calculator loop until I command it to stop? It only loops twice and it stops

print ('Welcome to acceleration calculator!')

def main( ):     
    iv = input('intial velocity (m/s): ')
    fv = input('final velocity (m/s): ') 
    t = input('time (s): ')
   
    a = ((float(fv)) - (float(iv))) / (float(t))

    def acceleration(a):
        return 'The acceleration is ' + str(a) + 'm/s²'

    print (acceleration(a))

main ()

again = input(" Would you like to calculate acceleration again? yes/no?: ")
if again == "yes" or again == "Yes" or again == "YES" or again == "y":
    main()
else:
    print("Thanks for using my calculator!")
    print('Acceleration calculator by Elias©')

New contributor

kachiko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

7

You are probably looking for the while loop to test if again is yes or not.

Any example of doing that could be:

def acceleration(fv, iv, t):
    return (float(fv) - float(iv)) / float(t)


def main():
    print("Welcome to acceleration calculator!")
    again = "yes"

    while any((again.casefold() == "y",
               again.casefold() == "yes")):

        iv = input("initial velocity (m/s): ")

        fv = input("final velocity (m/s): ")

        t = input("time (s): ")

        accel_result = acceleration(fv, iv, t)

        print(f"The acceleration is {accel_result}m/s²'")
        again = input("nWould you like to calculate acceleration again? yes/no?: ")

    print("Thanks for using my calculator!")

    print("Acceleration calculator by Elias©")


if __name__ == "__main__":
    main()

2

print ('Welcome to acceleration calculator!')

def main( ):     
    iv = input('intial velocity (m/s): ')
    fv = input('final velocity (m/s): ') 
    t = input('time (s): ')
   
    a = ((float(fv)) - (float(iv))) / (float(t))

    def acceleration(a):
        return 'The acceleration is ' + str(a) + 'm/s²'

    print (acceleration(a))

quit = False
while not quit:
    main ()
    again = input(" Would you like to calculate acceleration again? (yes/no?): ").lower()
    if again == 'no' or again == 'n':
        quit = True
        print("Thanks for using my calculator!")
        print('Acceleration calculator by Elias©')

3

You just need to put the logic in a loop. You can also separate all the input messages from the other necessary input logic by storing them in constants. Using map you can process all of your inputs to float on one line. Using an f-string you can eliminate the concatenation and explicit casting of strings in the result print. Your end messages can simply come after the while loop.

INIT = 'Would you like to calculate acceleration? yes/no?: '
IV = 'intial velocity (m/s): '
FV = 'final velocity (m/s): '
T  = 'time (s): '

def data(prompt:str) -> float:
    while True:
        try   : return float(input(prompt))
        except: ...

print ('Welcome to acceleration calculator!')

while input(INIT).lower() in ('yes', 'y'):
    iv, fv, t = map(data, (IV, FV, T))
    print (f'The acceleration is {(fv-iv)/t:.3e} m/s²')

print("Thanks for using my calculator!")
print('Acceleration calculator by Elias©')

2

You can arrange for your main function to return an indication of whether (or not) to continue asking for inputs.

You should also (always) properly validate user input. In your case values must be convertible to float and also be greater than zero.

So I suggest:

def getfloat(prompt: str) -> float:
    while True:
        try:
            if (x := float(input(prompt))) > 0:
                return x
        except ValueError:
            pass


def yesno() -> str:
    prompt = "Would you like to calculate acceleration again? (yes/no?): "
    while True:
        yn = input(prompt).lower()
        if yn in {"yes", "no", "y", "n"}:
            return yn


def main() -> str:
    iv = getfloat("intial velocity (m/s): ")
    fv = getfloat("final velocity (m/s): ")
    t = getfloat("time (s): ")
    a = (fv - iv) / t
    print(f"Acceleration={a:.2f} m/s²")
    return yesno()

if __name__ == "__main__":
    print("Welcome to acceleration calculator!")
    while main().startswith("y"):
        pass
    print("Thanks for using my calculator!")
    print("Acceleration calculator by Elias©")

here you can try this

print('Welcome to the acceleration calculator!')

def calculate_acceleration():
    iv = input('Initial velocity (m/s): ')
    fv = input('Final velocity (m/s): ')
    t = input('Time (s): ')

    try:
        # Calculate acceleration
        a = (float(fv) - float(iv)) / float(t)
        return 'The acceleration is ' + str(a) + ' m/s²'
    except ValueError:
        return 'Error: Please enter valid numbers for velocity and time.'
    except ZeroDivisionError:
        return 'Error: Time cannot be zero.'

def main():
    while True:
        # Call the function to calculate acceleration and print the result
        print(calculate_acceleration())

        # Ask if the user wants to calculate again
        again = input("Would you like to calculate acceleration again? yes/no?: ").strip().lower()
        if again not in {"yes", "y"}:
            break  # Exit the loop if the user doesn't want to continue

    print("Thanks for using my calculator!")
    print('Acceleration calculator by Elias©')

# Run the main function
main()

I’ve given below with better structure though there is nothing wrong in your approach. While learning the logic, parallelly you’ll know new things like f-string, using superscript in python. This is the best way of learning with least efforts.

# define the function
def main(iv, fv, t):
   
      
    a = (fv - iv)/t

    # you can use "msu207Bu00B2" for ms⁻²(optional)
    a = str(a) + "msu207Bu00B2" 

    return a


# a list of all possible values for 'yes'
ls = ['yes', 'Yes', 'YES', 'y']

print ('Welcome to acceleration calculator!') 

# create a while loop    
while True:
    again = input(" Would you like to calculate acceleration again? yes/no?: ")
    if again in ls: # if any value of 'yes' in list

        # convert the inputs into float directly
        iv = float(input('intial velocity (m/s): '))
        fv = float(input('final velocity (m/s): '))
        t = float(input('time (s): '))

        # function is called being inputs as arguments and printed
        r = main(iv, fv, t)

        # f-string to insert the returned value inside text
        print(f'The acceleration is {r}') 
    
    else: # if the user option is not 'yes'
        
        print("Thanks for using my calculator!")
        print('Acceleration calculator by Elias©')

        # important: Use break to break the loop
        break 

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật