0

I am making a physics 2d motion calculator that asks for a user input and adds that value to a variable in python. The problem I have is that the while loop I'm using has while V == 0: and if the user inputs 0, the program keeps asking for a new value for V. What can I do to make the program use the inputted value if the value is 0 and stop the program from asking again. Here is my code.

  while V == 0:
    V = float(input("What is the final velocity?"))
    if V == 0:
      pass

Here is the equation I want to use

    v = V - a*t

I would like to use the number 0 if the user inputs 0, but currently it just moves on and I don't want this.

v = 0
V = 0
d = 0
D = 0
t = 0
a = 0

if inp2 == "2": #solve for final velocity here
  print("We will solve for final velocity")
  while v == 0:
    v = float(input("What is the initial velocity?"))
  while a == 0:
    a = float(input("What is the acceleration?"))
    if a == 0:
      pass
  while t == 0:
    t = float(input("What is the total time"))
  V = v + a*t
  print ("The final velocity is"), V
5
  • 1
    If you don't want the program to loop when V == 0, then why is that your while condition? Commented Mar 15, 2018 at 18:14
  • Initially, I give values to each of the variables of 0 so the while loops will run. At the start of the program I always reassign all of the values in the variables to 0 so the while loops run. Is there a better way? Commented Mar 15, 2018 at 18:16
  • The variables are all initially assigned 0 as their value to run them through the while loop but if the user inputs 0 I want the program to save that as the value for the variable and continue to the next part of the program. @DavyM Commented Mar 15, 2018 at 18:22
  • Show more of your code so it is easier to understand what you're trying to accomplish. Commented Mar 15, 2018 at 18:23
  • If you want 0 to be an acceptable value, then set your variables to something else initially - such as None - and use that as the condition. Commented Mar 15, 2018 at 18:24

1 Answer 1

1

The while loops can be dropped if you don't care about validating the response. If you want to validate some of the responses (make sure they are numbers and are not zero), it's better to do it something like this.

def input_float(s, zero_allowed=False):
    while True:
        try:
            ans = float(input(s))
            if ans == 0 and not zero_allowed:
                print("Please enter a non-zero number")
            else:
                return ans
        except ValueError: # in case the user enters text
            print("Please enter a number")


if inp2 == "2": #solve for final velocity here
    print("We will solve for final velocity")
    v = input_float("What is the initial velocity?", zero_allowed=True)
    a = input_float("What is the acceleration?")
    t = input_float("What is the total time?")
    V = v + a * t
    print("The final velocity is", V)

This removes the need to initiate the values to zero.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.