Derek G.
Derek G.

Reputation: 63

Pounds to kilograms and grams conversion python function

I need to create a python function named poundsToMetric which converts weights given in pounds to kilograms and grams.

for example rather than print out 2.2 kilograms the correct answer would be 2 kilograms and 200 grams

to aid your work the following conversions hold:

1 pound = 2.2 kilograms 1 kilogram = 1000 grams

your program should prompt the user for the number of pounds and output the results in kilograms and grams.

def poundsToMetricFunction(kilograms, grams):
    pounds = float(input("enter the amount of pounds:  ")
    kilograms = pounds * 2.2
    grams = kilograms * 1000

    print('The amount of pounds you entered is ', pounds,
          ' This is ', kilograms, ' kilograms ', 'and', grams,
          'grams')

I know this isn't correct but I'm trying to figure out what I'm doing wrong I mean I know its probably all wrong but I'm new to this and I guess I just need some feedback on what I could add or if I have the correct information what format do I use for the correct syntax.

Upvotes: 2

Views: 40471

Answers (3)

tobias_k
tobias_k

Reputation: 82929

There are a few problems with your function:

  1. Of course, you still need to crop both kilograms and grams so the numbers do not "overlap". You can turn the one to int, thus dropping the decimal digits, and take the other modulo 1000 to drop everything in excess of one kilogram.
  2. Your syntax error seems to come from a missing ) in the input line.
  3. You conversion from pounds to kilograms is wrong, it should be / 2.2, not * 2.2
  4. Those function parameters make no sense; you calculate those inside the function.
  5. Instead, you should pass the pounds to the function and return the kilograms and grams and do the input and printing stuff outside of the conversion function.

Something like this:

def poundsToMetric(pounds):
    kilograms = pounds / 2.2
    grams = kilograms * 1000
    return int(kilograms), grams % 1000

pounds = float(input("How many Pounds? "))
kg, g = poundsToMetric(pounds)
print('The amount of pounds you entered is {}. '\
      'This is {} kilograms and {} grams.'.format(pounds, kg, g))

Upvotes: 2

sheepez
sheepez

Reputation: 1012

Just to fix your syntax I would offer (as others have mentioned the indentation is a bit off so I have fixed that too):

def poundsToMetricFunction(kilograms, grams):
    #You were missing a bracket on the following line
    pounds = float(input("enter the amount of pounds:  "))
    kilograms = pounds * 2.2
    grams = kilograms * 1000

    print('The amount of pounds you entered is ', pounds,
          ' This is ', kilograms, ' kilograms ', 'and', grams,
          'grams' )

If that is still not doing what you want you might need to give some more information about what you want it do. For example, the arguments that you are giving the function kilograms, grams are not doing anything at the moment.

Upvotes: 1

ndraca
ndraca

Reputation: 1

Your indentation is off, everything inside the function should be indented once more than the def. This is because everything indented after that call is a part of the function. Same rule goes for loops.

Second, don't float your input function, you can float your variables instead ie:

       kilograms = float(pounds) * 2.2

Third you need to make a function call. The function won't actually print anything until you give it the two arguments, kilograms and grams:

poundsToMetricFunction(20,30)

Upvotes: -1

Related Questions