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©')
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