Ask coding questions

← Back to all posts
what is wrong with this python? trying to make function which converts inch to centimeter
YvngBashit

def generate(inch_cent, arg2):
arg2 = ''
inch_cent = float(input('convert inch to centimeter :'))
total_value = inch_cent * arg2
print("Inch-Centimeter : ", total_value)
return total_value

print(generate())

Voters
dotcomboom
YvngBashit
Comments
hotnewtop
Nayoar

Hey,
Your function is much too complicated. Converting inches to centimetres is very easy:

The conversion parameter defaults to 2.54, but if specified in the function call, you can change how many centimetres per inch.
An even easier way of doing it but without the optional conversion:

The first one doesn't have a changeable conversion parameter, whereas the second one has a conversion parameter which you have to supply. If you don't know what a lambda function does, read this.

Hope you found this helpful
Upvote for vee freebucks!

hearpary

"Hi, I wanted to make a program that would convert cm to inch, but I get a TypeError:
print('Press the number of what you want to convert: ')
number = input()
inch = float(2.54)
if number == '1':
print('How many inches? ')
print('There are %s'.format(number / inch))

Here's all the code:

print('Welcome to the conversion program')
print('Which of these do you want to convert?')
print('How many centimeters in inches? 1""
""{\nHow many milliliters in a pint? 2""
""{\nHow many acres in a square mile? 3""
""{nHow many pounds in a metric ton? 4""
""{nHow many calories in a BTU? 5"")

print('Press the number of what you want to convert: ')
My friend tried to help, but it didn't work, he doesn't see the error either. We compared the values obtained in the program with the values on https://ruler.onl ."