diff --git a/README.md b/README.md index 3d3aeb0..fab011c 100644 --- a/README.md +++ b/README.md @@ -9,19 +9,19 @@ optimize and capitalize on what I and whoever else have and will learn. ## NUMBERS - [X] **Find PI to the Nth Digit** - Enter a number and have the program generate PI up to that many decimal places. Keep a limit to how far the program will go. -- [ ] **Find e to the Nth Digit** - Just like the previous problem, but with e instead of PI. Enter a number and have the program generate e up to that many decimal places. Keep a limit to how far the program will go. -- [ ] **Fibonacci Sequence** - Enter a number and have the program generate the Fibonacci sequence to that number or to the Nth number. -- [ ] **Prime Factorization** - Have the user enter a number and find all Prime Factors (if there are any) and display them. -- [ ] **Next Prime Number** - Have the program find prime numbers until the user chooses to stop asking for the next one. -- [ ] **Find Cost of the Tile to Cover W x H Floor** - Calculate the total cost of tile it would take to cover a floor plan of width and height, using a cost entered by the user. -- [ ] **Mortgage Calculator** - Calculate the monthly payments of a fixed term mortgage over given Nth terms at a given interest rate. Also figure out how long it will take the user to pay back the loan. For added complexity, add an option for users to select the compounding interval (Monthly, Weekly, Daily, Continually). -- [ ] **Change Return Program** - The user enters a cost and then the amount of money given. The program will figure out the change and the number of quarters, dimes, nickels, pennies needed for the change. -- [ ] **Binary to Decimal and Back Converter** - Develop a converter to convert a decimal number to binary or a binary number to its decimal equivalent. -- [ ] **Simple Calculator** - A simple calculator to do basic operators. -- [ ] **Unit Converter (temp, currency, volume, mass)** - Converts various units between one another. The user enters the type of unit being entered, the type of unit they want to convert to and then the value. The program will then make the conversion. +- [X] **Find e to the Nth Digit** - Just like the previous problem, but with e instead of PI. Enter a number and have the program generate e up to that many decimal places. Keep a limit to how far the program will go. +- [X] **Fibonacci Sequence** - Enter a number and have the program generate the Fibonacci sequence to that number or to the Nth number. +- [X] **Prime Factorization** - Have the user enter a number and find all Prime Factors (if there are any) and display them. +- [X] **Next Prime Number** - Have the program find prime numbers until the user chooses to stop asking for the next one. +- [X] **Find Cost of the Tile to Cover W x H Floor** - Calculate the total cost of tile it would take to cover a floor plan of width and height, using a cost entered by the user. +- [X] **Mortgage Calculator** - Calculate the monthly payments of a fixed term mortgage over given Nth terms at a given interest rate. Also figure out how long it will take the user to pay back the loan. For added complexity, add an option for users to select the compounding interval (Monthly, Weekly, Daily, Continually). +- [X] **Change Return Program** - The user enters a cost and then the amount of money given. The program will figure out the change and the number of quarters, dimes, nickels, pennies needed for the change. +- [X] **Binary to Decimal and Back Converter** - Develop a converter to convert a decimal number to binary or a binary number to its decimal equivalent. +- [X] **Simple Calculator** - A simple calculator to do basic operators. +- [X] **Unit Converter (temp, currency, volume, mass)** - Converts various units between one another. The user enters the type of unit being entered, the type of unit they want to convert to and then the value. The program will then make the conversion. - [ ] **Alarm Clock** - A simple clock where it plays a sound after X number of minutes/seconds or at a particular time. -- [ ] **Distance Between Two Cities** - Calculates the distance between two cities and allows the user to specify a unit of distance. This program may require finding coordinates for the cities like latitude and longitude. -- [ ] **Tax Calculator** - Asks the user to enter a cost and either a country or state tax. It then returns the tax plus the total cost with tax. +- [X] **Distance Between Two Cities** - Calculates the distance between two cities and allows the user to specify a unit of distance. This program may require finding coordinates for the cities like latitude and longitude. +- [X] **Tax Calculator** - Asks the user to enter a cost and either a country or state tax. It then returns the tax plus the total cost with tax. - [ ] **Credit Card Validator** - Takes in a credit card number from a common credit card vendor (Visa, MasterCard, American Express, Discoverer) and validates it to make sure that it is a valid number (look into how credit cards use a checksum). - [ ] **Factorial Finder** - The Factorial of a positive integer, n, is defined as the product of the sequence n, n-1, n-2, ...1 and the factorial of zero, 0, is defined as being 1. Solve this using both loops and recursion. - [ ] **Fast Exponentiation** - Ask the user to enter 2 integers a and b and output a^b (i.e. pow(a,b)) in O(lg n) time complexity. diff --git a/alarm-tone.wav b/alarm-tone.wav new file mode 100644 index 0000000..514d2d2 Binary files /dev/null and b/alarm-tone.wav differ diff --git a/alarm_clock.py b/alarm_clock.py new file mode 100644 index 0000000..4876e48 --- /dev/null +++ b/alarm_clock.py @@ -0,0 +1,35 @@ +# Program a simple alarm clock that plays a sound after a few minutes/seconds. +# +# Started: 6 March 2023 +# Completed: xx March 2023 + +# import time +# # from pydub import AudioSegment +# # from pydub.playback import play +# import sounddevice as sd +# import soundfile as sf + +# # song = AudioSegment.from_wav("alarm-tone.wav") + +# filename = 'alarm-tone.wav' +# data, fs = sf.read(filename, dtype='float32') + +# user_seconds = float(input("How many seconds do you want to wait before the alarm goes off? ")) +# user_minutes = float(input("How many minutes do you want to wait before the alarm goes off? ")) +# user_hours = float(input("How many hours do you want to wait before the alarm goes off? ")) + +# print("") +# timer_seconds = float(user_seconds + (user_minutes * 60) + (user_hours * 60 * 60)) + +# print("Waiting...") +# time.sleep(timer_seconds) +# # play(song) +# # print("Alarm went off...") +# sd.play(data, fs) +# status = sd.wait() + +from pydub import AudioSegment +from pydub.playback import play + +sound = AudioSegment.from_wav('alarm-tone.wav') +play(sound) diff --git a/binary_decimal.py b/binary_decimal.py new file mode 100644 index 0000000..df4e499 --- /dev/null +++ b/binary_decimal.py @@ -0,0 +1,37 @@ +# Program converts a decimal number to binary or a binary to it's decimal +# equivalent. +# +# Started: 19 January 2023 +# Completed: 24 January 2023 + +import math + +def deciToBin(decimal_num): + temp = int(decimal_num) + remainder = -1 + binary = '' + + while temp / 2 != 0: + remainder = temp % 2 + binary = str(remainder) + binary + temp = int(temp / 2) + + return binary + + +def binToDeci(binary_num): + temp = str(binary_num) + power = 0 + result = 0 + + while len(temp) > 0: + if temp[-1:] == "1": + result = result + math.pow(2,power) + + power += 1 + temp = temp.replace(temp, temp[:-1]) + + return result + +print("15 converted to binary format is %s" % deciToBin(15)) +print("1101 converted to decimal format is %d" % binToDeci(1101)) diff --git a/change_return.py b/change_return.py new file mode 100644 index 0000000..48bcee4 --- /dev/null +++ b/change_return.py @@ -0,0 +1,55 @@ +# User enter a cost and then the amount given. The program will figure out the +# change and the number of quarters, dimes, nickels, pennies needed for the change. +# +# Started: 18 January 2023 +# Completed: 18 January 2023 + +import math + +def changeCalulate(price, payment): + # Calculate the amount of change we need to make. Print it out to the user + temp_difference = int( math.ceil((payment - price) * 100)) + print("Change: $%.2f" % (temp_difference * 0.01)) + # Calculate quarter count + quarterCount = (temp_difference - (temp_difference % 25)) / 25 + temp_difference = temp_difference % 25 + # Calculate dime count + dimeCount = (temp_difference - (temp_difference % 10)) / 10 + temp_difference = temp_difference % 10 + # Calculate nickel count + nickelCount = (temp_difference - (temp_difference % 5)) / 5 + # Calculate penny count + pennyCount = temp_difference % 5 + # Print out the change and coin count + print("Your change is in %d quarters, %d dimes, %d nickels, and %d pennies." % (quarterCount, dimeCount, nickelCount, pennyCount)) + + +def checkPayment(price, payment): + # Check to make see if the payment is less than the price + # If so, ask for more money + if (payment < price): + print("That is not enough to cover the purchase.") + user_inp = input("Please enter more money: ") + addedMoney = float(user_inp) + payment += addedMoney + checkPayment(price, payment) + # Check to see if the payment is equal to the price. + # If so, no change necessary. Otherwise, payment is greater than the price + elif payment == price: + print("Thank you! No change necessary.") + else: + changeCalulate(price, payment) + +def main(): + # Ask the user for the price of the purchase. Cast into a float + user_inp = input("Purchase Price: ") + price = float(user_inp) + + # Ask the user for the amount they gave. Cast into a float + user_inp = input("How much are you giving? ") + user_money = float(user_inp) + + checkPayment(price, user_money) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/cost_of_tile.py b/cost_of_tile.py new file mode 100644 index 0000000..368c40c --- /dev/null +++ b/cost_of_tile.py @@ -0,0 +1,17 @@ +# Have the program calculate the total cost of tile it would take to cover +# a floor plan of x width and y height, using a cost entered by the user. +# +# Started: 15 January 2023 +# Completed: 15 January 2023 + +user_inp = input("How much do the tiles cost? ") +tile_price = float(user_inp) + +user_inp = input("What is the width of the area you're tiling? ") +area_width = float(user_inp) + +user_inp = input("What is the height of the area you're tiling? ") +area_height = float(user_inp) + +total_price = float(tile_price * (area_height * area_width)) +print("Total price for the area: $%.2f" % total_price) \ No newline at end of file diff --git a/dist_btwn_cities.py b/dist_btwn_cities.py new file mode 100644 index 0000000..29ea1ec --- /dev/null +++ b/dist_btwn_cities.py @@ -0,0 +1,65 @@ +# Write a program that calculates the distance between two cities and +# allows the user to specify a unit of distance. May require finding +# coordinates for the citie like latitude and longitude. +# +# Started: 07 March 2023 +# Completed: 25 April 2023 + +# Importing the geodesic module from the library +from geopy import distance +from geopy.distance import geodesic + +# Function for checking if the user entered city is in our dictionary +def check_city(city, flag_num): + if city in cities_dict: + flag_found_key[flag_num] = True + else: + print("Try a different city...") + +# Function to check if the user entered unit is in our list +def check_unit(unit): + return True if unit in units_of_dist else False + +# Function to calculate the distance using the geodesic function in geopy +# according to the unit the user requested +def calculate_dist(): + if user_unit.lower().__eq__("km") or user_unit.lower().__eq__("kilometers"): + print("%.3f kilometers" % geodesic(cities_dict[user_cities[0].upper()], cities_dict[user_cities[1].upper()]).km) + elif user_unit.lower().__eq__("m") or user_unit.lower().__eq__("meters"): + print("%.3f meters" % geodesic(cities_dict[user_cities[0].upper()], cities_dict[user_cities[1].upper()]).m) + else: + print("%.3f miles" % geodesic(cities_dict[user_cities[0].upper()], cities_dict[user_cities[1].upper()]).mi) + +# Created dictionary of top 10 major US cities and list of units of distance +cities_dict = { + "CHICAGO": [25.7617, -80.1918], + "DALLAS": [32.779167, -96.808891], + "HOUSTON": [29.749907, -95.358421], + "LOS ANGELES": [34.052235, -118.243683], + "NEW YORK": [40.730610, -73.935242], + "PHILADELPHIA": [39.952583, -75.165222], + "PHOENIX": [33.448376, -112.074036], + "SAN ANTONIO": [29.424349, -98.491142], + "SAN DIEGO": [32.715736, -117.161087], + "SAN JOSE": [37.335480, -121.893028] +} +units_of_dist = ["km", "kilometers", "m", "meters", "mi", "miles"] + +# Created flag array for checking cities and array of user cities +flag_found_key = [False, False] +flag_valid_unit = False +user_cities = ["", ""] + +# Loop through both flags and cities arrays to check and change flags accordingly +for x in range(0,2): + while not flag_found_key[x]: + user_cities[x] = input("Enter a US city: ") + check_city(user_cities[x].upper(), x) + +# Loop to accept the user's unit of distance and ask for +# another if the entered unit is invalid +while not flag_valid_unit: + user_unit = input("Enter a valid unit of measurement: ") + flag_valid_unit = check_unit(user_unit.lower()) + +calculate_dist() diff --git a/fibonacci_sequence.py b/fibonacci_sequence.py new file mode 100644 index 0000000..5299b91 --- /dev/null +++ b/fibonacci_sequence.py @@ -0,0 +1,29 @@ +# Enter a number and have the program generate the Fibonacci sequence to that +# number OR to the Nth number, whichever occurs first. +# +# Started: 14 January 2023 +# Completed: 14 January 2023 + +def fibonacci(user_num): + tracker = 3 + num1 = 0 + num2 = 1 + fib_num = num1 + num2 + print(num1) + print(num2) + print(fib_num) + while (user_num != fib_num) and (tracker != user_num): + num1 = num2 + num2 = fib_num + fib_num = num1 + num2 + tracker += 1 + print(fib_num) + if (user_num == fib_num): + print("Went to the number you input.") + else: + print("Went to the Nth number of the sequence.") + +user_inp = input("Enter a number: ") +user_num = int(user_inp) + +fibonacci(user_num) \ No newline at end of file diff --git a/mortgage_calculator.py b/mortgage_calculator.py new file mode 100644 index 0000000..22de4de --- /dev/null +++ b/mortgage_calculator.py @@ -0,0 +1,30 @@ +# Calculate the montly payments of a fixed term mortgage over given Nth terms +# at a given interest rate. Also, figure out how long it will take the user to +# pay back the loan. +# Added Complexity: add an option for users to select the compounding interval +# (monthly, weekly, daily, continually) +# +# Started: 15 January 2023 +# Completed: XX January 2023 + +import math +# Ask the user for the value of the mortgage +user_inp = input("What is the value of the mortgage? ") +mortgage_val = float(user_inp) + +# Ask the user for the interest rate +user_inp = input("What is the interest rate? ") +interest_rate = float(user_inp) + +# Ask the user the number of months of the loan +user_inp = input("How many months is the mortgage good for? ") +term_length = int(user_inp) + +# Calculate the monthly payments +monthly_interest_rate = float((interest_rate / 100) / 12) +print("Monthly Interest Rate: %f" % monthly_interest_rate) +monthly_interest_rate1 = float(1 + monthly_interest_rate) +monthly_payments = float((mortgage_val * (monthly_interest_rate * math.pow(1 + monthly_interest_rate, term_length)) / ( (math.pow(1 + monthly_interest_rate, term_length)) - 1 ))) + +# Print the monthly payments +print("Monthly Payments: $%.2f" % monthly_payments) diff --git a/next_prime_num.py b/next_prime_num.py new file mode 100644 index 0000000..a954ed2 --- /dev/null +++ b/next_prime_num.py @@ -0,0 +1,45 @@ +# Have the program find prime numbers until the user chooses to stop asking for the next one. +# +# Started: 14 January 2023 +# Completed: 14 January 2023 + +import math + +# Function to check whether the user wants the next prime number +def check_user(num_check): + if(num_check == 0): + user_inp = input("Would you like to see a prime number?(Y/N) ") + else: + user_inp = input("Would like to see the next prime number?(Y/N) ") + + if user_inp.capitalize() == 'Y': + nextPrime(num_check) + elif user_inp.capitalize() == 'N': + print("Goodbye!") + +# Function to print the next prime number +def nextPrime(num): + if (isPrime(num)): + print(num) + check_user(num+1) + else: + nextPrime(num+1) + pass + +# Function to check whether the current number is a prime number +def isPrime(num): + if num <= 1: + return False + if num <= 3: + return True + + if (num % 2 == 0 or num % 3 == 0): + return False + + for i in range(5, int(math.sqrt(num) + 1), 6): + if (num % i == 0 or num % (i + 2) == 0): + return False + + return True + +check_user(0) diff --git a/prime_factorization.py b/prime_factorization.py new file mode 100644 index 0000000..a666684 --- /dev/null +++ b/prime_factorization.py @@ -0,0 +1,31 @@ +# User enters a number. Have the program find all Prime Factors (if there are any) and +# display them. +# +# Started: 14 January 2023 +# Completed: 14 January 2023 + +# Function to find the prime factors of user input number +def prime_factorization(num): + # A list to keep track of prime factors + prime_factors = [] + # int variable to hold factorized number and to check when we reach 1 or a prime number + factorized_num = num + + # While loop to check when we hit 1 + while factorized_num != 1: + # For loop to go through prime numbers since user number can be any size. + # Will break out of for loop when we find the smallest prime number that can + # divided into user's num + for x in range(2,num): + if factorized_num % x == 0: + prime_factors.append(x) + factorized_num = factorized_num / x + break + # Print prime factors list for user's num + print("Prime Factors of", num, ":",prime_factors) + +# Ask user for number and cast it into an integer +user_inp = input("Enter a number: ") +user_num = int(user_inp) +# Call prime factorization method with user's number +prime_factorization(user_num) \ No newline at end of file diff --git a/simple_calculator.py b/simple_calculator.py new file mode 100644 index 0000000..182ac2c --- /dev/null +++ b/simple_calculator.py @@ -0,0 +1,54 @@ +# Program a simple calculator to do basic operators. +# +# Started: 09 February 2023 +# Completed: 3 March 2023 + +def add(num1, num2): + return num1 + num2 + +def subtract(num1, num2): + return num1 - num2 + +def multiply(num1, num2): + return num1 * num2 + +def divide(num1, num2): + if num2 == 0: + return "Can't divide by 0" + else: + return num1 / num2 + +def main(): + print("- Select Operation -") + print("1. Add") + print("2. Subtract") + print("3. Multiple") + print("4. Divide") + + while True: + choice = input("Enter number choice: ") + if choice in ('1', '2', '3', '4'): + try: + num1 = float(input("Enter first number: ")) + num2 = float(input("Enter second number: ")) + except ValueError: + print("Invalid input. Please enter a number.") + continue + + if choice == '1': + print(num1, "+", num2, "=", add(num1, num2)) + elif choice == '2': + print(num1, "-", num2, "=", subtract(num1, num2)) + elif choice == '3': + print(num1, "*", num2, "=", multiply(num1, num2)) + elif choice == '4': + print(num1, "/", num2, "=", divide(num1, num2)) + + next_calculation = input("Another calculation? (yes/no): ").lower() + if next_calculation == "no": + break + else: + print("Invalid Input.") + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/tax_calculator.py b/tax_calculator.py new file mode 100644 index 0000000..fba5d94 --- /dev/null +++ b/tax_calculator.py @@ -0,0 +1,159 @@ +# Program that asks the user to enter a cost and either a country or state tax. +# Return the tax plus the total cost with tax +# +# Using the state sales tax rate for each state found here: +# https://www.rocketmoney.com/learn/personal-finance/sales-tax-by-state +# Using the top 10 (excluding the US) countries with the highest population's +# sales tax numbers. Tax rates found here: +# https://take-profit.org/en/statistics/sales-tax-rate/#continents +# +# Started: 25 April 2023 +# Completed: XX February 2024 + + +# Function to check if the arg country is a valid country in dictionary +# or is the United States +def country_check(country): + if country.upper() in country_taxes: + return True + elif country.upper().__eq__("UNITED STATES") or country.upper().__eq__("US") or country.upper().__eq__("USA"): + return True + else: + print("Invalid country. Try again...\n") + return False + +# Function to check if the arg country is the United States +def USA_check(country): + if country.upper().__eq__("UNITED STATES") or country.upper().__eq__("US") or country.upper().__eq__("USA"): + return True + else: + return False + +# Function to check if the arg state is a valid US state +def state_check(state): + if state.upper() in state_taxes: + return True + else: + return False + +# Function to check if the arg purchase can be cast as a float without error +def check_purchase(purchase): + try: + float(purchase) + return True + except ValueError: + return False + +# Function to return the state the user enters their purchase is from. +# Loops until a valid state is entered +def get_state(): + user_state = input("Enter what state your purchase is in: ") + while not state_check(user_state.upper()): + print("Not a valid US state.") + user_state = input("Enter what state your purchase is in: ") + + return user_state.upper() + +# Function to return the tax of the given country or US state +# Calls get_state() to grab the US state to return +def get_tax(country): + if country.upper().__eq__("UNITED STATES") or country.upper().__eq__("US") or country.upper().__eq__("USA"): + us_state = get_state() + return state_taxes[us_state.upper()] + else: + return country_taxes[country.upper()] + +# Function to calculate total purchase given args subtotal and tax +def get_total_purchase(subtotal, tax): + return subtotal + (subtotal * (tax/100)) + +# Created dictionaries for the sales taxes for the individual states in the US +# and the other top 9 highly populated countries +state_taxes = { + "ALABAMA": 4.00, + "ALASKA": 0, + "ARIZONA": 5.60, + "ARKANSAS": 6.50, + "CALIFORNIA": 7.25, + "COLORADO": 2.90, + "CONNECTICUT": 6.35, + "D.C.": 6.00, + "FLORIDA": 6.00, + "GEORGIA": 4.00, + "HAWAII": 4.00, + "IDAHO": 6.00, + "ILLINOIS": 6.25, + "INDIANA": 7.00, + "IOWA": 6.00, + "KANSAS": 6.50, + "KENTUCKY": 6.00, + "LOUISIANA": 4.45, + "MAINE": 5.50, + "MARYLAND": 6.00, + "MASSACHUSETTS": 6.25, + "MICHIGAN": 6.00, + "MINNESOTA": 6.88, + "MISSISSIPPI": 7.00, + "MISSOURI": 4.23, + "NEBRASKA": 5.50, + "NEVADA": 6.85, + "NEW JERSEY": 6.63, + "NEW MEXICO": 5.00, + "NEW YORK": 4.00, + "NORTH CAROLINA": 4.75, + "NORTH DAKOTA": 5.00, + "OHIO": 5.75, + "OKLAHOMA": 4.50, + "PENNSYLVANIA": 6.00, + "RHODE ISLAND": 7.00, + "SOUTH CAROLINA": 6.00, + "SOUTH DAKOTA": 4.50, + "TENNESSEE": 7.00, + "TEXAS": 6.25, + "UTAH": 6.10, + "VERMONT": 6.00, + "VIRGINIA": 5.30, + "WASHINGTON": 6.50, + "WEST VIRGINIA": 6.00, + "WISCONSIN": 5.00, + "WYOMING": 4.00 +} +country_taxes = { + "CHINA": 13, + "INDIA": 18, + "INDONESIA": 11, + "PAKISTAN": 17, + "NIGERIA": 7.5, + "BRAZIL": 17, + "BANGLADESH": 15, + "RUSSIA": 20, + "MEXICO": 16 +} + +# Created flags to know if country is valid and if the country is the USA +flag_valid_country = False +flag_is_USA = False +tax = 0.0 + +# Gather user input for the purchase total +user_input = input("Enter how much your purchase is: ") +while not check_purchase(user_input): + print("\nNot a valid purchase quantity") + user_input = input("Enter how much your purchase is: ") + +# Conver the input from str to float +purchase = float(user_input) + +# Loop until user enters a valid country in our list OR the United States +# Change flag var to True when found +while not flag_valid_country: + user_country = input("Enter the country your purchase is in: ") + flag_valid_country = country_check(user_country.upper()) + flag_is_USA = USA_check(user_country.upper()) + +# Find the tax for the given country or US state +tax = get_tax(user_country) + +# Print out final purchase total and the tax percentage +print("Tax = %.2f%%" % (tax)) +print("Purchase total: $%.2f" % get_total_purchase(purchase, tax)) diff --git a/unit_converter.py b/unit_converter.py new file mode 100644 index 0000000..ae79f58 --- /dev/null +++ b/unit_converter.py @@ -0,0 +1,89 @@ +# Program that converts various units between one another. User +# enters the type of unit being entered, the type of unit they want +# to convert to and then the value. Then the program makes the conversion. +# Convert temp, currency, volume, and mass +# +# Started: 3 March 2023 +# Completed: 3 March 2023 + +unit_dict = { + "USD": 1.0, + "EUR": 0.94185, + "GBP": 0.83362, + "cubic meter": 1000.0, + "cubic centimeter": 0.001, + "cubic feet": 35.31, + "millilitre": 0.001, + "litre": 1.0, + "gallon": 3.785, + "gram": 1.0, + "kilogram": 1000.0, + "milligram": 0.001, + "ton": 1000000.0, + "pound": 453.592, + "ounce": 28.3495 +} + +UNIT_SELECTIONS = [ + "celsius", + "fahrenheit", + "kelvin", + "USD", + "EUR", + "GBP", + "cubic meter", + "cubic centimeter", + "cubic feet", + "millilitre", + "litre", + "gallon", + "gram", + "kilogram", + "milligram", + "ton", + "pound", + "ounce" +] + +temp_units = ["celsius", "fahrenheit"] +currency_units = ["USD", "EUR", "GBP"] +volume_units = ["cubic meter", "cubic centimeter", "millilitre", "litre", "gallon"] +mass_units = ["gram", "kilogram", "milligram", "ton", "pound", "ounce"] + +while True: + input_unit = input("Enter the type of unit you want to convert: ") + output_unit = input("Enter the type of unit you want to convert into: ") + + if input_unit not in UNIT_SELECTIONS: + print("Invalid input unit. Please try another unit.\n") + continue + elif output_unit not in UNIT_SELECTIONS: + print("Invalid output unit. Please try another unit.\n") + continue + + input_val_str = input("Enter the value you want to convert: ") + input_val = float(input_val_str) + + can_convert = [ + input_unit in temp_units and output_unit in temp_units, + input_unit in currency_units and output_unit in currency_units, + input_unit in volume_units and output_unit in volume_units, + input_unit in mass_units and output_unit in mass_units + ] + + if any(can_convert): + output_val = 0.0 + if (input_unit == "celsius") and (output_unit == "fahrenheit"): + output_val = (input_val * 1.8) + 32 + elif (input_unit == "fahrenheit") and (output_unit == "celsius"): + output_val = (input_val - 32) * (5/9) + else: + output_val = (input_val * unit_dict[input_unit] / unit_dict[output_unit]) + + print("%.2f %s = %.2f %s\n" % (input_val, input_unit, output_val, output_unit)) + else: + print("Invalid conversion attempt. Try different units.\n") + + input_check = input("Would you like to do another conversion? (yes/no): ") + if (input_check.lower() == "no"): + break