Jarrett
Jarrett

Reputation: 21

TypeError: not all arguments converted during string formatting python 3

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()
#

Upvotes: 2

Views: 1508

Answers (2)

Sudesh
Sudesh

Reputation: 100

  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

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249133

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)

Upvotes: 3

Related Questions