In this tutorial, we’ll learn how to easily create weight converter in Python. We’ll go through a proper practical Python code example in order for better understanding. The complete source code will also be provided in the end.
Outline
- Introduction: Weight Converter In Python
- Creating Weight Converter In Python (Easy Example)
- Step 1: Take Input From User
- Step 2: Code To Convert Value to Kilograms
- Step 3: Code To Convert Value to Pounds
- Step 4: Display Message If Value Is Not Valid
- Step 5: Test The Program
- Source Code Of Weight Converter In Python
- Conclusion
Introduction: Weight Converter In Python
As the name suggests, this Python program will be used to convert and display a weight which is entered by a user. In this program, an input will be taken by the user. If the user wants to convert kilograms to pounds then enter the specified character for kilograms and if it want to convert pounds to kilograms then enter the specified character for pounds.
Concepts that are used in this program are discussed in our previous articles, so don’t forget to check them out as well. Let’s now jump into its practical implementation.
Creating Weight Converter In Python (Easy Example)
Follow below steps for detailed practical understanding.
Step 1: Take Input From User
weightValue= int(input('Enter the weight:')) unitValue=input('Enter (L) for lbs and (K) for kilograms:')
We’ll take input then convert it to an integer and finally store it in a variable. We’ll also take a character to be used as a condition where it’ll decide whether to convert the value into pounds or kilograms. We’ll be using Python if else statement for this. Click here if you want to learn Python if else concept in detail.
Step 2: Code To Convert Value to Kilograms
if unitValue.lower() == 'l': convertedValue=weightValue*0.45 print(f'Your converted value is {convertedValue} kilograms')
This code will first convert the value to lower using the lower method of Python string and then check it if the value matches with specified value of right operand. If true then it’ll convert the value to kilograms and display it using a formatted string. Click here if you want to learn how to format a Python string.
Step 3: Code To Convert Value to Pounds
elif unitValue.lower() == 'k': convertedValue=weightValue/0.45 print(f'Your converted value is {convertedValue} pounds')
This code will also convert the value to lower and check it if the value matches with the specified character. If yes then it’ll convert the value to pounds and display it.
Step 4: Display Message If Value Is Not Valid
else: print('Wrong input')
If the inputted character is not matched with the specified characters then else block will run which will display the text ‘Wrong input’.
The complete source code of weight converter in Python is given in the below section.
Step 5: Test The Program
Test 1
Enter the weight: 58 Enter (L) for lbs and (K) for kilograms: l Your converted value is 26.1 kilograms
Test 2
Enter the weight: 89 Enter (L) for lbs and (K) for kilograms: k Your converted value is 197.77777777777777 pounds
Source Code Of Weight Converter In Python
weightValue= int(input('Enter the weight:')) unitValue=input('Enter (L) for lbs and (K) for kilograms:') if unitValue.lower() == 'l': convertedValue=weightValue*0.45 print(f'Your converted value is {convertedValue} kilograms') elif unitValue.lower() == 'k': convertedValue=weightValue/0.45 print(f'Your converted value is {convertedValue} pounds') else: print('Wrong input')
So this is how we can easily create a program to convert weight in Python. Hope you’ve learned a lot from this post. Do implement it in your project and share your experience with us. Also, don’t forget to share it with your squad of Python developers.
Feel free to ask if you still have any doubts/queries regarding the implementation of weight converter in Python. We’ll be super happy to solve all your queries.
Conclusion
To conclude this tutorial, hope you now have an in-depth knowledge of how to easily create a weight converter in Python. Your valuable feedback will be well appreciated.
Do visit our other posts on Python programming. Thank you for reading this article.