I am learning Python now. It is too complicated. I started the file that i did but it crashed. Can you show me how to do it?

Write a Python program to convert a given weight from kg to pound and to gal and to liter.

1kg=0.2642gal=2.205lb
1gal = 3.785liter

It means when i open the file using python. I just put a number and hit enter. I will get something like:
123kg=.....lb=....gal=....l

Thank you

You can do something like this.

number = input("Enter a number ")
#number entered is in kilograms.
converted_to_lbs = number * 2.205
print(converted_to_lbs)

there is a little difference between the input statement in python 2.3 and 3.x version. I may have got mixed up when I wrote the example above. Hope this helps.

Will it be the same to convert to gallon and liter? Because I want when i put a number and hit ENTER. I will get the result is a line with all conversions from kg to lbs, to gallon, to liters. This si kind of hard for me because I only know is just like put one number and it convert from kg to lbs. I don't know how to input many commands, so I can get the answer with a line:

>>>123
>>>123kgs=...lbs=....gallons=...liters

This one didn't work. This is what I put

print 'Welcome to our weight conversion program!'
# prompt for a number and confirm it
x = raw_input ('Enter a weight : ')
h = fx*2.205 lbs = fx*0.2642gal=fx*0.2642*3.785l
print 'h'

This one didn't work. This is what I put

print 'Welcome to our weight conversion program!'
# prompt for a number and confirm it
x = raw_input ('Enter a weight : ')
h = fx*2.205 lbs = fx*0.2642gal=fx*0.2642*3.785l
print 'h'

what is 'fx'?
Here's what you should do.
x is the number that the user has entered.
define and print the following variables.

x_L = x * <conversion factor for Liters>
x_lbs = x * <conversion factor for lbs>
print(x_L)
print(x_lbs)

so the number you entered will first be converted from L to gal. Next, the program will assume that the number you entered is in kilograms and convert that number to lbs.

Edited 6 Years Ago by mahela007: n/a

I couldn't run the program. All I did is:

print 'Welcome to our weight conversion program!'
# prompt for a number and confirm it
x=raw_input ('Enter a weight')
x_lbs = x * 2.205 lbs
x_gal = x * 0.2642 gal
x_L = x * 1.000 L
print (x_lbs) = (x_gal) = (x_L)

Note that kg and pound are weight units of measurement.
Gallon and liter are volume units of measurement.
Don't mix them!

You can use this code:

kg = float(raw_input("Enter number of kg: "))
lb = kg * 2.205
print kg, "kg =", lb, "pounds"

Or you can use this code:

gallon = float(raw_input("Enter number of gallons: "))
liter = gallon * 3.785
print gallon, "gallons =", liter, "liters"

Or you can use a generic value:

value = float(raw_input("Enter a number: "))
lb = value * 2.205
print value, "kg =", lb, "pounds"
liter = value * 3.785
print value, "gallons =", liter, "liters"

Edited 6 Years Ago by bumsfeld: n/a

this is the one it worked after i tried many times:

print 'Welcome to our weight conversion program!'
# prompt for a number and confirm it
z=raw_input ('Enter a weight: ')
x=float(z)
xlbs = x * 2.205
xgal = x * 0.2642
xL = x * 1.000
print x, '=', xlbs, 'pounds', '=', xgal, "gallons", '=', xL, "liters"
y=raw_input ('press enter to quit')

This question has already been answered. Start a new discussion instead.