I'm a beginner to Python and I'm challenging myself to make a calculator that calculates the area of shapes based on values given by the user. I'm stuck on how I can make it change the measurement based on the users selection. For example, if a user picked square feet as their main measurement and then wanted to convert it to square meters, how would I be able to change the value from square feet to square meters and vice versa?
I've only accomplished meters to feet as doing all combinations would be time consuming. I was wondering if there is an easier way to do it rather than making code for every combination of a possible choice? Here's what I've tried, where 'Choice2' is where I am stuck;
ChoiceI = int(input(Fore.RESET + "\nPick the measurement:\nFeet (1), Meters (2), Inches (3) "))
Meters = "m2"
Feet = "ft2"
Inches = "inches"
if ChoiceI == 1:
Width = int(input("\nWhat is the width of the rectangle? "))
if Width >= 1:
Length = int(input("\nWhat is the length of the rectangle? "))
if Length >= 1:
Area = Width * Length
print("The area of the rectangle is", round(Area), "inch")
if ChoiceI == 2:
Width = int(input("\nWhat is the width of the rectangle? "))
if Width >= 1:
Length = int(input("\nWhat is the length of the rectangle? "))
if Length >= 1:
Area = Width * Length
print("The area of the rectangle is", round(Area), "m2")
if ChoiceI == 3:
Width = int(input("\nWhat is the width of the rectangle? "))
if Width >= 1:
Length = int(input("\nWhat is the length of the rectangle? "))
if Length >= 1:
Area = Width * Length
print("The area of the rectangle is", round(Area), "ft2")
Choice2 = input("\nDo you want to convert the measurement? (y/n) ")
if Choice2 == 'y':
Convert = int(input("\nChoose the measurement:\nFeet (1), Metres (2), Inches (3)"))
if Convert == 1:
print("The area of the rectangle is", round(Area), "feet")
elif Choice2 == 'n':
print("The area of the rectangle is", round(Area), Meters)
For 'Choice2'; How can I make it change what is prints based on what the user chose?
The only thing that changes is the unit of measurement (UOM). The height and width values are the same regardless of the UOM.
You can eliminate most of your repetitive code as follows:
Example: