2

Ballistic calculator for Computer science class. I can't figure out why it keeps giving the following error message :

travel_time = range % velocity
TypeError: not all arguments converted during string formatting


print ("Welcome to the Laird Industries Ballistic Calculator")
range = raw_input("What is the approximate distance to your target? (m)")
velocity = raw_input("What is the muzzle velocity of the projectile? ")

def time_to_target():
    travel_time = range % velocity
    print "Travel duration {0}".format(travel_time)
#
time_to_target()
#

Thanks for the info. Fixed code :

range_to_target = raw_input("What is the approximate distance to your target? (m)")
velocity = raw_input("What is the muzzle velocity of the projectile? ")

def time_to_target():
    travel_time = float(range_to_target) / float(velocity)
    print "Travel duration {0}".format(travel_time)
#
time_to_target()
#
1
  • Are you sure you want % (the remainder operator) rather than / (the division operator) here? Commented Oct 15, 2014 at 13:25

2 Answers 2

3

It's because range is a string (you got it from input()) and % for strings in Python is a special formatting operator. You just need to convert to a numeric data type, something like this:

travel_time = float(range) % float(velocity)
Sign up to request clarification or add additional context in comments.

Comments

0
  1. raw_input() returns user input as string. So your range and velocity are string and so you can not use % operator. So convert range and velocity to float.

  2. range is a predefined in python. So you should not use it as variable name

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.