Search code examples
pythontypeerrorpositional-argument

Got an TypeError: conversion() missing 3 required positional arguments When i was coding


When I test this program I get a TypeError: conversion() missing 3 required positional arguments: 'milesPerhour1', 'milesPerhour2', and 'milesPerhour3'

So I tried organizing my code so everything related to the conversion function was all in one place, but that didn't work.

Update: Pasted entire code

def main():
  print("This is a Coversion app the converts Miles Per Hour to Kilometer Per Hour.")
  print("1 mile per hour is 1.60934 kilometers per hour.")
   
  milesPerhour1 = int(input("Type a positive integer value of MPH you would see on the high way or in your neighborhood. "))

  milesPerhour2 = int(input("Type another. "))

  milesPerhour3 = int(input("Type one last one. "))

  return milesPerhour1, milesPerhour2, milesPerhour3


def conversion(milesPerhour1, milesPerhour2, milesPerhour3):
  conversionNumber = 1.60934

  conversion1 = conversionNumber*milesPerhour1

  conversion2 = conversionNumber*milesPerhour2

  conversion3 = conversionNumber*milesPerhour3

  return conversion1, conversion2, conversion3


def print2(milesPerhour1, milesPerhour2, milesPerhour3, conversion1, conversion2, conversion3):
  print(str(milesPerhour1) + " Miles Per Hour is " + str(conversion1) + " Kilometers Per Hour.")
  print(str(milesPerhour2) + " Miles Per Hour is " + str(conversion2) + " Kilometers Per Hour.")
  print(str(milesPerhour3) + " Miles Per Hour is " + str(conversion3) + " Kilometers Per Hour.")
  


main()
conversion()
print2()

Solution

  • You need to pass 3 arguments into the conver() function. The conver() function doesn't know the value of mPh1, mPh2, mPh3 because those are defined in a different function, so you need to put those into the parenthesis when you call the function.

    On a side note, main() returns a tuple, so you probably want conver() to accept just 1 argument (a tuple) instead of 3.

    EDIT:

    When you define a function like this:

    def foo(n1, n2, n3):
      --some_code--
    

    requesting 3 parameters (n1, n2, n3), you need to provide 3 arguments (so values for those parameters) when you call the function, like this:

    foo(3, 5, 7)