|
| 1 | +#Matt Nichols |
| 2 | +#HB2 Lab02 |
| 3 | + |
| 4 | +#Version 1 |
| 5 | + |
| 6 | +#Variable for ft to meters |
| 7 | +ft = 0.3048 |
| 8 | + |
| 9 | +#While loop to allow the user to continue to put in different calculations |
| 10 | +while True: |
| 11 | + user_input = input("Enter a number in feet to convert to meters\nType 'done' to exit\n") |
| 12 | + if user_input == "done": |
| 13 | + print("Okay, thanks for your time ") |
| 14 | + break |
| 15 | + #Try and except for converter. |
| 16 | + try: |
| 17 | + converter = float(user_input) |
| 18 | + calc = converter * ft |
| 19 | + print(f'{user_input} in feet = {calc} in meters') |
| 20 | + except: |
| 21 | + print("Sorry, that input was invalid. Please enter numerals 0-9\nMake sure there are no spaces in between your numerals ") |
| 22 | + |
| 23 | +#Version 2 |
| 24 | + |
| 25 | +#Dictionary with measurements for future converting |
| 26 | +unit_converter = { |
| 27 | +'ft': 0.3048, |
| 28 | +'mi': 1609.34, |
| 29 | +'m': 1, |
| 30 | +'km': 1000 |
| 31 | +} |
| 32 | + |
| 33 | +#While loop for the user to continue putting in different calculations |
| 34 | +while True: |
| 35 | + |
| 36 | +#User input for either exiting the program or pulling something from the dictionary |
| 37 | + unit_input = input("Enter your unit of measurement in 'ft', 'mi', 'm', or 'km' to convert to meters:\nType 'done' to exit\n") |
| 38 | + |
| 39 | + #Exit |
| 40 | + if unit_input == 'done': |
| 41 | + result = "Okay, thank you for your time" |
| 42 | + print(result) |
| 43 | + break |
| 44 | + |
| 45 | + #unit converter/calculator/easy out if user input is invalid with try and except |
| 46 | + try: |
| 47 | + converter = unit_converter[unit_input] |
| 48 | + numbers = float(input("Now enter the length of measurement:\n")) |
| 49 | + calculator = numbers * converter |
| 50 | + print(f'{numbers} {unit_input} = {calculator} in meters' ) |
| 51 | + except: |
| 52 | + print("Oops, something went wrong. Make sure that you type the options that are presented\nFirst section - Type 'ft', 'mi', 'm', 'km', or 'done'.\nSecond section - Type numerals 0-9 and ensure there are no spaces between your numerals") |
| 53 | + |
| 54 | +#Version 3 |
| 55 | + |
| 56 | +#Dictionary with measurements for future converting |
| 57 | +unit_converter = { |
| 58 | +'ft': 0.3048, |
| 59 | +'mi': 1609.34, |
| 60 | +'m': 1, |
| 61 | +'km': 1000, |
| 62 | +'yard': 0.9144, |
| 63 | +'inch': 0.0254 |
| 64 | +} |
| 65 | + |
| 66 | +#While loop for the user to continue putting in different calculations |
| 67 | +while True: |
| 68 | + |
| 69 | +#User input for either exiting the program or pulling something from the dictionary |
| 70 | + unit_input = input("Enter your unit of measurement in 'ft', 'mi', 'm', 'km', 'yard', or 'inch' to convert to meters:\nType 'done' to exit\n") |
| 71 | + |
| 72 | + #Exit |
| 73 | + if unit_input == 'done': |
| 74 | + result = "Okay, thank you for your time" |
| 75 | + print(result) |
| 76 | + break |
| 77 | + |
| 78 | + #unit converter/calculator/easy out if user input is invalid with try and except |
| 79 | + try: |
| 80 | + converter = unit_converter[unit_input] |
| 81 | + numbers = float(input("Now enter the length of measurement:\n")) |
| 82 | + calculator = numbers * converter |
| 83 | + print(f'{numbers} {unit_input} = {calculator} in meters' ) |
| 84 | + except: |
| 85 | + print("Oops, something went wrong. Make sure that you type the options that are presented\nFirst section - Type 'ft', 'mi', 'm', 'km', or 'done'.\nSecond section - Type numerals 0-9 and ensure there are no spaces between your numerals") |
| 86 | + |
0 commit comments