cursetv3
cursetv3

Reputation: 11

How can I change a value based on a users selection?

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?

Upvotes: 1

Views: 198

Answers (2)

Adon Bilivit
Adon Bilivit

Reputation: 26825

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:

UMAP = {
    '1': ('inches', 'in'),
    '2': ('feet', 'ft'),
    '3': ('metres', 'm')
}

while (m := input('Select unit of measurement 1) inches, 2) feet, 3) metres, 9) exit: ')) != '9':
    if (unit := UMAP.get(m)):
        try:
            w = float(input(f'Enter width in {unit[0]}: '))
            h = float(input(f'Enter height in {unit[0]}: '))
            print(f'Area of rectangle is {w*h:.2f}{unit[1]}\u00B2')
        except ValueError:
            print('Invalid height/width')
    else:
        print('Invalid option')

Example:

Select unit of measurement 1) inches, 2) feet, 3) metres, 9) exit: 2
Enter width in feet: 4
Enter height in feet: 3.5
Area of rectangle is 14.00ft²
Select unit of measurement 1) inches, 2) feet, 3) metres, 9) exit: 9

Upvotes: 2

NoobQuestions1
NoobQuestions1

Reputation: 57

You could make a dictionary like this:

dict={'measurement unit1':'measurement unit 1's conversion formula',
      'measurement unit2':'measurement unit 2's conversion formula'
}

Example:

measure_dict={'meters - inches':39.37*<meter units>
              'inches - meters': 0.0254 *<inches units>
              }

Further, i've cleaned up your code and implemented the dict to see it working. You could do the same with every other measurement above and any other measures you wish to implement.

ChoiceI = int(input("\nPick the measurement:\nFeet (1), Meters (2), Inches (3) "))
measure = {
    1: 39.37 * ChoiceI,
    2: 0.0254 * ChoiceI,
    3: 0.3048 * ChoiceI,
}
while 0 < ChoiceI <= 3:
    Width = int(input("\nWhat is the width of the rectangle?  "))
    if ChoiceI == 1:
        if Width >= 1:
            Length = int(input("\nWhat is the length of the rectangle?  "))
            if Length >= 1:
                Area = Width * Length
                print(f"The area of the rectangle is {round(Area)} inch")
    elif ChoiceI == 2:
        if Width >= 1:
            Length = int(input("\nWhat is the length of the rectangle?  "))
            if Length >= 1:
                Area = Width * Length
                print(f"The area of the rectangle is {round(Area)} m2")
    elif Width >= 1:
        Length = int(input("\nWhat is the length of the rectangle?  "))
        if Length >= 1:
            Area = Width * Length
            print(f"The area of the rectangle is {round(Area)} ft2")
    Choice2 = input("\nDo you want to convert the measurement? (y/n) ")
    if Choice2 == "y":
        ChoiceI
        print(f"The area of the rectangle is { measure[ChoiceI]} feet")
    elif Choice2 == "n":
        print(f"The area of the rectangle is {round(Area)} meters")
    else:
        print("Invalid choice!")
print("Invalid choice!")

To avoid multiple if/elifs, if you have python 3.11, you could use match case statement.

The code is untested, but should get the job done

Upvotes: 1

Related Questions